Pixel multiplication gives different result
In detail I read a image in c++ and multiplied its pixel of 3 channel BGR value with 255, It gives me resultant image as negative image. In this case, if you are multiplying a pixel value with 255, it should be saturated to white have to give [255, 255, 255]. But negative image have different values. The following code is,
uchar *p;
for (int i = 0; i < nRows; i++){
p = image.ptr<uchar>(i);
for (int j = 0; j < nCols; j++){
p[j] = table[p[j]] * 255;
//cout << (int)p[j] * 255 <<" ";
}
}
So I test a user-defined Mat with one pixel value of 3 channel BGR value with 255, producing result of [255, 255, 255].
Mat sample = Mat(1, 1, CV_8UC3);
uchar *l;
l = sample.data;
for (int i = 0; i < sample.rows * sample.cols; i++){
cout << sample * 255;
cout << sample;
}
well, the proper way to invert the colors would be:
Scalar::all(255) - image
not by multiplying.your test code is broken (you never initialize the Mat's values, so the result is arbitrary)
please explain the relevance of the
table
in the 1st snippet, and why you useuchar*
for what is obviously a Vec3b (BGR) image.First thing i am not trying to inverse the image. I am new to openCV, So in a intention to workout the tutorials, I trying to multiply a pixel by a value to break down the problem (from the tutorials for masking http://docs.opencv.org/2.4/doc/tutori...). Then i got inverse image of input image. Next thing table is just reducing the image value or like round up. I use uchar* to access image array continuously like just shown in the tutorials.
as a general advice - please stay away from per-pixel loops, and use high-level, optimized algorithms instead.
and that tutorial is a bit unlucky, it has nothing to with masks, and in the end, you should indeed use filter2D, not try your own convolution.
Okay berak! Thanks...But i just wanted to know. For a intuition Multiplying every pixels with 255 must give a white image right?
well, almost. any pixel with 0 value will stay unchanged.
in your test code, it should be:
Done what you have said. Now I noticed that after multiplication values exceeds 255. So saturation must take place. According to algorithm I(x,y)= min (max (round (r), 0), 255) in this page [http://docs.opencv.org/2.4/modules/co...] resultant value should be 255. So white image have to come. Still we have negative image here. Why?
In algorithm round is just going to give the same number right?
You use unsigned char which ranges from 0 to 255 and write the uchar value back into the uchar matrix... When you multiply eg.: 255 * 255 weired stuff gonna happen since the value exceeds 255. Just write the value to eg.: a CV_16UC1 Matrix and it should work.
I assumed that, to create a image(Mat object) with CV_16UC1 type that you have specified and assigned the manipulated old Mat matrix value to new image Mat matrix value. Still not working. However if value exceed 255 than i must saturate to 255, producing a image of white.