OpenCV - remap() - getting black pixels - c++

I'm using an STMap to map a .jpg image using remap().
I loaded my STMap, split the channels and converted each channel matrix to CV_32FC1.
I checked them and it worked - each matrix displays correctly and all of its values are between 0.0 and 1.0.
However, when i try to use the remap() function:
Mat dst;
remap(image4, dst,map_x,map_y,INTER_LINEAR,BORDER_CONSTANT,Scalar(0,0,0));
imshow( "Result", dst );
It just displays a black image.
image4 = my .jpg image
map_x = grayscale CV_32FC1 (red channel
of the original STMap)
map_y = grayscale CV_32FC1 (green channel
of the original STMap)
What could be the problem?
Thanks!

Black image when using cv::remap is due to using offsets instead of absolute locations in the passed map(s).
Optical flow algorithms usually export motion vectors, not absolute positions, whereas cv::remap expects the absolute coordinate (subpixel) to sample from.
To convert between the two, starting with a CV_32FC2 flow matrix we can do something like this:
// Convert from offsets to absolute locations.
Mat mapx(flow.size(), CV_32FC1);
Mat mapy(flow.size(), CV_32FC1);
for (int row = 0; row < flow.rows; row++)
{
for (int col = 0; col < flow.cols; col++)
{
Point2f f = flow.at<Point2f>(row, col);
mapx.at<float>(row, col) = col + f.x;
mapy.at<float>(row, col) = row + f.y;
}
}
Then mapx and mapy can be used in remap.

Related

Convert grayscale image to single color in OpenCV

I'm wondering if there is a way to convert a grayscale image to one color image? Like if I have an image in grayscale and I want to convert it to shades of blue instead? Is that possible in OpenCV?
Thank you very much!
According to the opencv community answer, you should of creating a 3-channel image by yourself.
Mat empty_image = Mat::zeros(src.rows, src.cols, CV_8UC1);//initial empty layer
Mat result_blue(src.rows, src.cols, CV_8UC3); //initial blue result
Mat in1[] = { ***GRAYINPUT***, empty_image, empty_image }; //construct 3 layer Matrix
int from_to1[] = { 0,0, 1,1, 2,2 };
mixChannels( in1, 3, &result_blue, 1, from_to1, 3 ); //combine image
After that, you can get your blue channel image. Normally, the blue channel of an colour image in opencv is the first layer (cuz they put 3 channels as BGR).
By the way, if you wanna use the copy each pixel method, you can initial an empty image
Mat result_blue(src.rows, src.cols, CV_8UC3); //blue result
for (int i =0; i<src.rows; i++)
for (int j=0; j<src.cols; j++){
Vec3b temp = result_blue.at<Vec3b>(Point(i,j));//get each pixel
temp[0] = gray.at<uchar>(i,j); //give value to blue channel
result_blue.at<Vec3b>(Point(x,y)) = temp; //copy back to img
}
However, it will take longer as there are two loops!
A gray scale image is usually just one dimensional. Usually what I do if I want to pass in a gray scale image into a function that accepts RGB (3-dimensional), I replicate the the matrix 3 times to create a MxNx3 matrix. If you wish to only use the Blue channel, just concat MxN of zeros in the 1st dimension and the 2nd dimension while putting the original gray scale values in the 3rd dimension.
To accomplish this you would essentially just need to iterate over all the pixels in the grayscale image and map the values over to a color range. Here is pseudo-code:
grayImage:imageObject;
tintedImage:imageObject;
//Define your color tint here
myColorR:Int = 115;
myColorG:Int = 186;
myColorB:Int = 241;
for(int i=0; i<imagePixelArray.length; i++){
float pixelBrightness = grayImage.getPixelValueAt(i)/255;
int pixelColorR = myColorR*pixelBrightness;
int pixelColorG = myColorG*pixelBrightness;
int pixelColorB = myColorB*pixelBrightness;
tintedImage.setPixelColorAt(i, pixelColorR, pixelColorG, pixelColorB);
}
Hope that helps!

Normalising an image in opencv

I have a RGB image stored in a Mat datastructure. I am converting the image into grayscale using cvtColor function in opencv. After that I am trying to normalise the image to the range [0,1]. I am using the default normalize function of opencv. To check the correctness, I tried printing the pixel values and equate it with matlab values(Matlab values are already in the range [0,1]). But the values differ a lot. Help me to make both results almost same. Below are the opencv and matlab codes.
Mat img1 = imread("D:/input.png", CV_LOAD_IMAGE_COLOR);
cvtColor(img1, img1, CV_BGR2GRAY);
img1.convertTo(img1, CV_32FC1);
cv::normalize(img1, img1, 0.0, 1.0, NORM_MINMAX, CV_32FC1);
for (int i = 0; i < img1.rows; i++)
{
for (int j = 0; j < img1.cols; j++)
{
cout << img1.at<float>(i, j) << endl;
}
}
Matlab code:
I=im2double(imread('input.png'));
gI=rgb2gray(I);
display(gI)
I don't think you want to normalize here. The Matlab conversion rgb2gray uses this equation: 0.2989 * R + 0.5870 * G + 0.1140 * B. So there's no expectation that you have the minimum value of 0.0 or the maximum value of 1.0 in your output greyscale image. You would only expect 0 and 1 if you had pure white (255,255,255) and pure black (0,0,0) pixels.
Try this:
img *= 1./255;
cvtColor(img, img, CV_BGR2GRAY);

Trying to implement a tiny part of matlab code in C++ using opencv

I am trying to convert an image to double precision using opencv. I am trying to imitate the im2double function available in MATLAB in c++. So, for this what i did was..
Mat IMG = imread("lena.bmp");
Size size(8,8);
Mat img,img_re,grey;
cvtColor( IMG, img, CV_BGR2GRAY );
resize(img,img_re,size);
img_re.convertTo( grey, CV_64FC3, 1.0/255.0 );
std::cout<<grey<<std::endl;
unsigned char *input = (unsigned char*)(grey.data);
grey: [0.3764705882352941, 0.5176470588235293, 0.4352941176470588, 0.8274509803921568;
0.392156862745098, 0.5254901960784314, 0.7372549019607844, 0.6431372549019607;
0.4431372549019608, 0.6431372549019607, 0.7176470588235294, 0.5607843137254902;
0.5333333333333333, 0.3254901960784314, 0.6862745098039216, 0.8431372549019608]
The data stored in grey is almost similar to the data obtained from matlab. the pixels have a range of [0,1]here. But ,my problem starts here. I want to now access the pixel values from 'grey' and save it to a boost matrix.
So for this i use..
for (unsigned i=0; i < height; ++i)
{
for (unsigned j=0; j < width; ++j )
{
image(i,j) = input[grey.step * j + i ];
}
}
image:: [4,4]((24,24,144,144),(24,24,144,144),(24,216,144,224),(24,63,144,63))
After this step all the values in the matrix have a range of [0,255]. grey scale images are between [0,255] but why do it get the values between [0,1] in the first case.
please stay away from accessing Mat's raw 'data' pointer, and use:
grey.at<double>(i,j);
instead.
also, if im_re is a 1 channel, grayscale image, your typeflag is wrong, should be:
img_re.convertTo( grey, CV_64F, 1.0/255.0 );

Accessing certain pixel's intensity value(grayscale image) in openCV

I just realised that there is nothing on the web, after much searching about how to access a pixel's intensity value in OpenCv. A grayscale image.
Most online searches are about how to access BGR values of a colour image, like this one: Accessing certain pixel RGB value in openCV
image.at<> is basically for 3 channels, namely the BGR, out of curiousity, is there another similar method from OpenCV of accessing a certain pixel value of a grayscale image?
You can use image.at<uchar>(j,i) to acces a pixel value of a grayscale image.
cv::Mat::at<>() function is for every type of image, whether it is a single channel image or multi-channel image. The type of value returned just depends on the template argument provided to the function.
The value of grayscale image can be accessed like this:
//For 8-bit grayscale image.
unsigned char value = image.at<unsigned char>(row, column);
Make sure to return the correct data type depending on the image type (8u, 16u, 32f etc.).
For IplImage* image, you can use
uchar intensity = CV_IMAGE_ELEM(image, uchar, y, x);
For Mat image, you can use
uchar intensity = image.at<uchar>(y, x);
at(y,x)]++;
for(int i = 0; i < 256; i++)
cout<<histogram[i]<<" ";
// draw the histograms
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound((double) hist_w/256);
Mat histImage(hist_h, hist_w, CV_8UC1, Scalar(255, 255, 255));
// find the maximum intensity element from histogram
int max = histogram[0];
for(int i = 1; i < 256; i++){
if(max < histogram[i]){
max = histogram[i];
}
}
// normalize the histogram between 0 and histImage.rows
for(int i = 0; i < 255; i++){
histogram[i] = ((double)histogram[i]/max)*histImage.rows;
}
// draw the intensity line for histogram
for(int i = 0; i < 255; i++)
{
line(histImage, Point(bin_w*(i), hist_h),
Point(bin_w*(i), hist_h - histogram[i]),
Scalar(0,0,0), 1, 8, 0);
}
// display histogram
namedWindow("Intensity Histogram", CV_WINDOW_AUTOSIZE);
imshow("Intensity Histogram", histImage);
namedWindow("Image", CV_WINDOW_AUTOSIZE);
imshow("Image", image);
waitKey();
return 0;
}

accessing pixel value of gray scale image in OpenCV

I just want to get my concept clear that - is accessing all the matrix elements of cv::Mat means I am actually accessing all the pixel values of an image (grayscale - 1 channel and for colour - 3 channels)? Like suppose my code for printing the values of matrix of gray scale that is 1 channel image loaded and type CV_32FC1, is as shown below, then does that mean that I am accessing only the members of the cv::mat or I am accessing the pixel values of the image (with 1 channel - grayscale and type CV_32FC1) also?
cv::Mat img = cv::imread("lenna.png");
for(int j=0;j<img.rows;j++)
{
for (int i=0;i<img.cols;i++)
{
std::cout << "Matrix of image loaded is: " << img.at<uchar>(i,j);
}
}
I am quite new to image processing with OpenCV and want to clear my idea. If I am wrong, then how can I access each pixel value of an image?
You are accessing the elements of the matrix and you are accessing the image itself also. In your code, after you do this:
cv::Mat img = cv::imread("lenna.png");
the matrix img represents the image lenna.png. ( if it is successfully opened )
Why don't you experiment yourself by changing some of the pixel values:
cv::Mat img = cv::imread("lenna.png");
//Before changing
cv::imshow("Before",img);
//change some pixel value
for(int j=0;j<img.rows;j++)
{
for (int i=0;i<img.cols;i++)
{
if( i== j)
img.at<uchar>(j,i) = 255; //white
}
}
//After changing
cv::imshow("After",img);
Note: this only changes the image values in volatile memory, that is where the mat img is currently loaded. Modifying the values of the mat img, not going to change value in your actual image "lenna.png",which is stored in your disk, (unless you do imwrite)
But in case of 1-channel grayscale image it is CV_8UC1 not CV_32FC1
In order to get the pixel value of the grayscale image (an integer between 0 and 255), the answer also needs to be typecasted.
int pixelValue = (int)img.at<uchar>(i,j);