compare value pixel with int - c++

I have a Mat Dist (CV_8U) done by distanceTransform.
Now I have to check each coordinate of Dist is > 0 and modify the value of another Mat M = Mat :: zeros
the code is
int main(){
....
for(i=0;i<Dist.rows;++i)
{
for(j=0;j<Dist.cols;++j)
{
if(Dist.at<uchar>(i,j) > 0){
M.at<uchar>(i,j)=2;
}
}
}
....
}
but I error cv :: exception.
I looked in the documentation and elsewhere , I tried to change from uchar to vec3b . I modified the exception in visual studio 2015 but nothing .
Where am I wrong?

The function distanceTransform does not return ad CV_8U, it is CV_32 as we can see in the documentation:
dst – Output image with calculated distances. It is a 32-bit
floating-point, single-channel image of the same size as src .
So the code should not read uchar, but float
...
if(Dist.at<float>(i,j) > 0.f)
...
In case you used the labels from distance transform, in the documentation we have the following:
labels – Optional output 2D array of labels (the discrete Voronoi
diagram). It has the type CV_32SC1 and the same size as src . See the
details below.
So, in this case you should access it as an int
...
if(Dist.at<int>(i,j) > 0)
...
I hope this helps you.
By the way, maybe an epsilon value instead of the 0 will be better....

Related

OpenCV - RGB Channels in Float Data Type and Intensity Range within 0-255

How can I achieve the values of the RGB channels as
Float data type
Intensity range within 0-255
I used CV_32FC4 as the matrix type since I'll perform floating-point mathematical operations to implement Daltonization. I was expecting that the intensity range is the same with the intensity range of the RGB Channels in CV_8UC3, just having a different data type. But when I printed the matrix I noticed that the intensities of the channels are not within 0-255. I realized that it due to the range of the float matrix type.
Mat mFrame(height, width, CV_32FC4, (unsigned char *)pNV21FrameData);
for(int y = 0 ; y < height ; y++){
for(int x = 0 ; x < width ; x++){
Vec4f BGRA = mFrame.at<Vec4f>(y,x);
// Algorithm Implementation
mFrame.at<Vec4f>(y,x) = BGRA;
}
}
Mat mResult;
mFrame.convertTo(mResult, CV_8UC4, 1.0/255.0);
I need to manipulate the pixels like BGRA[0] = BGRA[0] * n; then assign it back to the matrix.
By your comments and the link in it I see that the data comes in BGRA. The data is in uchar.
I assume this from this line:
Mat mResult(height, width, CV_8UC4, (unsigned char *)poutPixels);
To solve this you can create the matrix and then convert it to float.
Mat mFrame(height, width, CV_8UC4, (unsigned char *)pNV21FrameData);
Mat mFloatFrame;
mFrame.convertTo(mFloatFrame, CV_32FC4);
Notice that this will keep the current ranges (0-255) if you need another one (like 0-1) you may put the scaling factor.
Finally you can convert back, but beware that this function does saturate_cast. If you have an specific way you want to manage the overflow or the decimals, you will have to do it before converting it.
Mat mResult;
mFloatFrame.convertTo(mResult, CV_8UC4);
Note that 1.0/255.0 is not there, since the data is already in the range of 0-255 (at least before the operations).
One final comment, the link in your comments use IplImage and other old C (deprecated) versions of OpenCv. If you are working in c++, stick to the c++ versions like Mat. This is not in the code you show here, but in the you linked. This comment is more for you to avoid future headaches.

Access pixel value in a cvMat image in OpenCV

I have used Canny edge detector to successfully identify the edges of a given image. I'm struggling with finding specific points on this detected edge line.
My approach:
I used the cv::canny function in opencv and the output is stored in cv::Mat format. I want to iterate through the all values of the matrix and identify all those pixels where the edge is present so that I can detect the specific points on the detected edge line.
Function used:
cv::Canny(frame_gray,contours,50,150);
The output is stored in contours and it is of type CV_8UC3
To access the pixel value, have tried
contours.at<int>(i,j) != 0
and also
contours.at<uchar>(i,j) != 0
Will greatly appreciate help in the above. If the approach is correct and am missing something or else if i should try another approach
Thanks
Edit:
for(int i=0;i<img_width;i++)
{
if((int)contours.at<uchar>(i,neckcenter.y) > 0 )
{
Point multipoints(i,neckcenter.y);
circle( contours, multipoints, neckpoint, Scalar( 255, 0, 0 ),4, 8, 0 );
cout << (int)contours.at<uchar>(i,neckcenter.y) << endl;
}
}
I am using the above code which forms a small circle of radius 1 (defined by neckpoint) where it detects a point on and edge. The neckcenter.y is a constant value derived from an earlier calculation. What am i doing wrong here ?
Output of the code -
you probably want a grayscale pass before applying Canny:
Mat gray;
cvtColor(bgr,gray,CV_BGR2GRAY); // now gray is a 8bit, uchar Mat
Mat contours;
cv::Canny(gray,contours,50,150);
// now you're safe to use:
uchar value = contours.at<uchar>(i,j);
The syntax:
contours.at<uchar>(i,j)
Is correct for your case in terms of data type (i.e. a grayscale image). The problem is possibly hinted at by this line:
for(int i=0;i<img_width;i++)
When you access OpenCV pixels using at, you must specify the pixel position as (row, col), so your indexing is the wrong way round. Try this in all places where you access pixels:
contours.at<uchar>(j,i)
From the OpenCV documentation:
You have a 3 channel image of the type unsigned char. To access it you should use the cv::Vec3b type. Here is how to do it:
int channel = 0;//or 1 or 2
contours.at<cv::Vec3b>(i,j)[channel]
To check if all elements are 0:
contours.at<cv::Vec3b>(i,j)[0]==0 && contours.at<cv::Vec3b>(i,j)[1]==0 && contours.at<cv::Vec3b>(i,j)[2]==0
But where do you have the information that the image type of contours is CV_8UC3 ?

building an image from Mat object in OpenCV

Im using OpenCV and I have a Mat object of size 1024*1024(extracted from a photo and manipulated) and the values are in the range [1..25].for example:
Mat g;
g=[1,5,2,14,13,5,22,24,5,13....;
21,12,...;
..
.];
I want to represent these values as an image.It is only an illustration image to show the different areas,each area with a color.
For example: all the values that equals 1=red, all the values that equals 14=blue, and so on..
and then construct and display this photo.
Anybody have an idea how should i proceed?
Thanks!
If you are not too fussed what colors you get, you can scale your data (so it almost fills the 0 to 255 range) then use an inbuilt colormap.
e.g.
cv::Mat g = ...
cv::Mat image;
cv::applyColorMap(g * 10, image, COLORMAP_RAINBOW);
See the applyColorMap() doco
there are colormaps , but they won't help if your data is in the [0..25] range only. so you probably ned to roll your own version of that:
Vec3b lut[26] = {
Vec3b(0,0,255),
Vec3b(13,255,11),
Vec3b(255,22,1),
// all the way down, you get the picture, no ?
};
Mat color(w,h,CV_8UC3);
for ( int y=0; y<h; y++ ) {
for ( int x=0; x<w; x++ ) {
color.at<Vec3b>(y,x) = lut[ g.at<uchar>(y,x) ];
// check the type of "g" please, i assumed CV_8UC1 here.
// if it's CV_32S, use g.at<int> , i.e, you need the right type here
}
}

IMAGE COMPARISON USING OPENCV code from poitr

HI i found this code to compare images
cv::Mat img1 = ...
cv::Mat img2 = ...
cv::Mat result = ...
int threshold = (double)(img1.rows * img1.cols) * 0.7;
cv::compare(img1 , img2 , result , cv::CMP_EQ );
int similarPixels = countNonZero(result);
if ( similarPixels > threshold ) {
cout << "similar" << endl;
}
but since i m new to OPENCV i dont know what are the values for " cv::Mat img1=..."
please help me out and tried out the code with the image path as value but its giving an error
void compare(const MatND& src1, const MatND& src2, MatND& dst, int cmpop)
compare function takes 4 parametres and this are ;
•src1 – The first source array
•src2 – The second source array; must have the same size and same type as src1
•value – The scalar value to compare each array element with
•dst – The destination array; will have the same size as src1 and type= CV_8UC1
And as i understand this image has some fiducial points like coordinate system ( X / Y )
such as ;
1 45 123
2 56 164
3 64 147
and with these points are storing in array and then it send arrays to compare function in order to find matchers .
If you want to work with image comparasion my advise is you can search feret database which is free to use for research.
You can use imread to load an image to cv::Mat img1, e.g.
cv::Mat img1 = imread("c:\\test.bmp"); // To load an RGB image

Using Mat::at(i,j) in opencv for a 2-D Mat object

I am using Ubuntu 12.04 and OpenCV 2
I have written the following code :
IplImage* img =0;
img = cvLoadImage("nature.jpg");
if(img != 0)
{
Mat Img_mat(img);
std::vector<Mat> RGB;
split(Img_mat, RGB);
int data = (RGB[0]).at<int>(i,j)); /*Where i, j are inside the bounds of the matrix size .. i have checked this*/
}
The problem is I am getting negative values and very large values in the data variable. I think I have made some mistake somewhere. Can you please point it out.
I have been reading the documentation (I have not finished it fully.. it is quite large. ) But from what I have read, this should work. But it isnt. What is going wrong here?
Img_mat is a 3 channeled image. Each channel consists of pixel values uchar in data type.
So with split(Img_mat, BGR) the Img_mat is split into 3 planes of blue, green and red which are collectively stored in a vector BGR. So BGR[0] is the first (blue) plane with uchar data type pixels...hence it will be
int dataB = (int)BGR[0].at<uchar>(i,j);
int dataG = (int)BGR[1].at<uchar>(i,j);
so on...
You have to specify the correct type for cv::Mat::at(i,j). You are accessing the pixel as int, while it should be a vector of uchar. Your code should look something like this:
IplImage* img = 0;
img = cvLoadImage("nature.jpg");
if(img != 0)
{
Mat Img_mat(img);
std::vector<Mat> BGR;
split(Img_mat, BGR);
Vec3b data = BGR[0].at<Vec3b>(i,j);
// data[0] -> blue
// data[1] -> green
// data[2] -> red
}
Why are you loading an IplImage first? You are mixing the C and C++ interfaces.
Loading a cv::Mat with imread directly would be more straight-forward.
This way you can also specify the type and use the according type in your at call.