Increment the value of each pixel of an image
I have an image. I want to modify the image in such a way that the value of each pixel is increased by a particular margin. Next I want to save the newly created image and display it.
I tried changing each pixel value but was only able to set it to constant value. I don't want all the pixels to be constant but their value should increase by (lets say) 50.
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
int main()
{
Mat image;
image = imread("/home/rohit_136/Desktop/image.jpg",CV_LOAD_IMAGE_UNCHANGED);
for (int i = 0; i < image.cols; i++) {
for (int j = 0; j < image.rows; j++) {
Vec3b intensity = image.at<Vec3b>(j, i)=50
}
}
return 0;
}
c++ image c++11 opencv3.0
add a comment |
I have an image. I want to modify the image in such a way that the value of each pixel is increased by a particular margin. Next I want to save the newly created image and display it.
I tried changing each pixel value but was only able to set it to constant value. I don't want all the pixels to be constant but their value should increase by (lets say) 50.
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
int main()
{
Mat image;
image = imread("/home/rohit_136/Desktop/image.jpg",CV_LOAD_IMAGE_UNCHANGED);
for (int i = 0; i < image.cols; i++) {
for (int j = 0; j < image.rows; j++) {
Vec3b intensity = image.at<Vec3b>(j, i)=50
}
}
return 0;
}
c++ image c++11 opencv3.0
The code you wrote won't compile. When posting code, try to post actual code.
– Yakk - Adam Nevraumont
Mar 22 '17 at 19:55
add a comment |
I have an image. I want to modify the image in such a way that the value of each pixel is increased by a particular margin. Next I want to save the newly created image and display it.
I tried changing each pixel value but was only able to set it to constant value. I don't want all the pixels to be constant but their value should increase by (lets say) 50.
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
int main()
{
Mat image;
image = imread("/home/rohit_136/Desktop/image.jpg",CV_LOAD_IMAGE_UNCHANGED);
for (int i = 0; i < image.cols; i++) {
for (int j = 0; j < image.rows; j++) {
Vec3b intensity = image.at<Vec3b>(j, i)=50
}
}
return 0;
}
c++ image c++11 opencv3.0
I have an image. I want to modify the image in such a way that the value of each pixel is increased by a particular margin. Next I want to save the newly created image and display it.
I tried changing each pixel value but was only able to set it to constant value. I don't want all the pixels to be constant but their value should increase by (lets say) 50.
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
int main()
{
Mat image;
image = imread("/home/rohit_136/Desktop/image.jpg",CV_LOAD_IMAGE_UNCHANGED);
for (int i = 0; i < image.cols; i++) {
for (int j = 0; j < image.rows; j++) {
Vec3b intensity = image.at<Vec3b>(j, i)=50
}
}
return 0;
}
c++ image c++11 opencv3.0
c++ image c++11 opencv3.0
edited Mar 22 '17 at 19:54
Yakk - Adam Nevraumont
187k20198382
187k20198382
asked Mar 22 '17 at 19:05


RohitRohit
815
815
The code you wrote won't compile. When posting code, try to post actual code.
– Yakk - Adam Nevraumont
Mar 22 '17 at 19:55
add a comment |
The code you wrote won't compile. When posting code, try to post actual code.
– Yakk - Adam Nevraumont
Mar 22 '17 at 19:55
The code you wrote won't compile. When posting code, try to post actual code.
– Yakk - Adam Nevraumont
Mar 22 '17 at 19:55
The code you wrote won't compile. When posting code, try to post actual code.
– Yakk - Adam Nevraumont
Mar 22 '17 at 19:55
add a comment |
2 Answers
2
active
oldest
votes
Vec3b
is a vector which contains 3 bytes (chars). Each byte denotes the value of an individual BGR (blue, green, red) or RGB channel. You should traverse this vector and modify each channel independently. Be careful, because we are talking about bytes, each byte should take a value between 0 and 255. I suggest setting a threshold for avoiding overflow.
Note: if you don't care about alpha channel, I suggest loading your image with CV_LOAD_IMAGE_COLOR. This will ensure that your image is loaded in the BGR format.
add a comment |
cv::Mat Image =cv::imread("image.jpg");
uint8_t * orig_ptr = (uint8_t*)Image.data;
for (int y = 0; y < Image.rows; y++)
{
for (int x = 0; x < Image.cols; x++)
{
/*Reading Pizel Values*/
int R = orig_ptr[x * 3 + y*Image.step + 2];/*R -Pixel*/
int G = orig_ptr[x * 3 + y*Image.step + 1];/*G-Pixel*/
int B = orig_ptr[x * 3 + y*Image.step]; /*B-Pixel*/
/*Updating Values*/
orig_ptr[x * 3 + y*Image.step + 2] = cv::saturate_cast<uint8_t>(R + 50);
orig_ptr[x * 3 + y*Image.step + 1] =cv::saturate_cast<uint8_t>(G + 50);
orig_ptr[x * 3 + y*Image.step]=cv::saturate_cast<uint8_t>(B + 50);
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42960582%2fincrement-the-value-of-each-pixel-of-an-image%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Vec3b
is a vector which contains 3 bytes (chars). Each byte denotes the value of an individual BGR (blue, green, red) or RGB channel. You should traverse this vector and modify each channel independently. Be careful, because we are talking about bytes, each byte should take a value between 0 and 255. I suggest setting a threshold for avoiding overflow.
Note: if you don't care about alpha channel, I suggest loading your image with CV_LOAD_IMAGE_COLOR. This will ensure that your image is loaded in the BGR format.
add a comment |
Vec3b
is a vector which contains 3 bytes (chars). Each byte denotes the value of an individual BGR (blue, green, red) or RGB channel. You should traverse this vector and modify each channel independently. Be careful, because we are talking about bytes, each byte should take a value between 0 and 255. I suggest setting a threshold for avoiding overflow.
Note: if you don't care about alpha channel, I suggest loading your image with CV_LOAD_IMAGE_COLOR. This will ensure that your image is loaded in the BGR format.
add a comment |
Vec3b
is a vector which contains 3 bytes (chars). Each byte denotes the value of an individual BGR (blue, green, red) or RGB channel. You should traverse this vector and modify each channel independently. Be careful, because we are talking about bytes, each byte should take a value between 0 and 255. I suggest setting a threshold for avoiding overflow.
Note: if you don't care about alpha channel, I suggest loading your image with CV_LOAD_IMAGE_COLOR. This will ensure that your image is loaded in the BGR format.
Vec3b
is a vector which contains 3 bytes (chars). Each byte denotes the value of an individual BGR (blue, green, red) or RGB channel. You should traverse this vector and modify each channel independently. Be careful, because we are talking about bytes, each byte should take a value between 0 and 255. I suggest setting a threshold for avoiding overflow.
Note: if you don't care about alpha channel, I suggest loading your image with CV_LOAD_IMAGE_COLOR. This will ensure that your image is loaded in the BGR format.
answered Mar 22 '17 at 20:28


Ervin SzilagyiErvin Szilagyi
1,4012517
1,4012517
add a comment |
add a comment |
cv::Mat Image =cv::imread("image.jpg");
uint8_t * orig_ptr = (uint8_t*)Image.data;
for (int y = 0; y < Image.rows; y++)
{
for (int x = 0; x < Image.cols; x++)
{
/*Reading Pizel Values*/
int R = orig_ptr[x * 3 + y*Image.step + 2];/*R -Pixel*/
int G = orig_ptr[x * 3 + y*Image.step + 1];/*G-Pixel*/
int B = orig_ptr[x * 3 + y*Image.step]; /*B-Pixel*/
/*Updating Values*/
orig_ptr[x * 3 + y*Image.step + 2] = cv::saturate_cast<uint8_t>(R + 50);
orig_ptr[x * 3 + y*Image.step + 1] =cv::saturate_cast<uint8_t>(G + 50);
orig_ptr[x * 3 + y*Image.step]=cv::saturate_cast<uint8_t>(B + 50);
}
}
add a comment |
cv::Mat Image =cv::imread("image.jpg");
uint8_t * orig_ptr = (uint8_t*)Image.data;
for (int y = 0; y < Image.rows; y++)
{
for (int x = 0; x < Image.cols; x++)
{
/*Reading Pizel Values*/
int R = orig_ptr[x * 3 + y*Image.step + 2];/*R -Pixel*/
int G = orig_ptr[x * 3 + y*Image.step + 1];/*G-Pixel*/
int B = orig_ptr[x * 3 + y*Image.step]; /*B-Pixel*/
/*Updating Values*/
orig_ptr[x * 3 + y*Image.step + 2] = cv::saturate_cast<uint8_t>(R + 50);
orig_ptr[x * 3 + y*Image.step + 1] =cv::saturate_cast<uint8_t>(G + 50);
orig_ptr[x * 3 + y*Image.step]=cv::saturate_cast<uint8_t>(B + 50);
}
}
add a comment |
cv::Mat Image =cv::imread("image.jpg");
uint8_t * orig_ptr = (uint8_t*)Image.data;
for (int y = 0; y < Image.rows; y++)
{
for (int x = 0; x < Image.cols; x++)
{
/*Reading Pizel Values*/
int R = orig_ptr[x * 3 + y*Image.step + 2];/*R -Pixel*/
int G = orig_ptr[x * 3 + y*Image.step + 1];/*G-Pixel*/
int B = orig_ptr[x * 3 + y*Image.step]; /*B-Pixel*/
/*Updating Values*/
orig_ptr[x * 3 + y*Image.step + 2] = cv::saturate_cast<uint8_t>(R + 50);
orig_ptr[x * 3 + y*Image.step + 1] =cv::saturate_cast<uint8_t>(G + 50);
orig_ptr[x * 3 + y*Image.step]=cv::saturate_cast<uint8_t>(B + 50);
}
}
cv::Mat Image =cv::imread("image.jpg");
uint8_t * orig_ptr = (uint8_t*)Image.data;
for (int y = 0; y < Image.rows; y++)
{
for (int x = 0; x < Image.cols; x++)
{
/*Reading Pizel Values*/
int R = orig_ptr[x * 3 + y*Image.step + 2];/*R -Pixel*/
int G = orig_ptr[x * 3 + y*Image.step + 1];/*G-Pixel*/
int B = orig_ptr[x * 3 + y*Image.step]; /*B-Pixel*/
/*Updating Values*/
orig_ptr[x * 3 + y*Image.step + 2] = cv::saturate_cast<uint8_t>(R + 50);
orig_ptr[x * 3 + y*Image.step + 1] =cv::saturate_cast<uint8_t>(G + 50);
orig_ptr[x * 3 + y*Image.step]=cv::saturate_cast<uint8_t>(B + 50);
}
}
answered Jan 1 at 6:57
niranjan reddyniranjan reddy
213
213
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42960582%2fincrement-the-value-of-each-pixel-of-an-image%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The code you wrote won't compile. When posting code, try to post actual code.
– Yakk - Adam Nevraumont
Mar 22 '17 at 19:55