Reputation: 207
Here's my code. When I execute it. Given any arbit image. It always gives result as an entirely black or entirely white image. Is there any problem with the iteration?
int main(void)
{
int i=0,j=0,height=0,width=0,step=0,k=0;
Mat img_hsv,img_rgb,red_blob,blue_blob;
//reading image... rgb format(default)... CV_8U3C
img_rgb = imread("pic.png",1);
//converting rgb image to hsv format for applying further operations
cvtColor(img_rgb,img_hsv,CV_BGR2HSV);
//defining the various component values or rather the pointer to those
//components of HSV format... hue, sat and value
uchar img_h=*(img_hsv.data+i*img_hsv.step+j*img_hsv.elemSize());
uchar img_s=*(img_hsv.data+i*img_hsv.step+j*img_hsv.elemSize()+1);
uchar img_v=*(img_hsv.data+i*img_hsv.step+j*img_hsv.elemSize()+2);
//naming window to be displayed
//for(i=0;i<1000;i++){j=i;cout<<img_h;if(img_h==170)cout<<"yesss";}
namedWindow("win1", CV_WINDOW_AUTOSIZE);
Mat img_bw(img_hsv.rows,img_hsv.cols,CV_8U);
imshow("win1", img_hsv);
//applying threshold and hence applying conversions
//by iterating over the entire image//
for(i=0;i<img_hsv.rows;i++){
for(j=0;j<img_hsv.cols;j++){
if((img_h>120)) && (img_s>150 || img_s<25) && (img_v>150))
*(img_bw.data+i*img_bw.step+j*img_bw.elemSize())=255;
else *(img_bw.data+i*img_bw.step+j*img_bw.elemSize())=0;
}
}
imshow("win1", img_bw);
Thanks for the reply in advance!!!
Upvotes: 0
Views: 2375
Reputation: 2842
I guess i've already answered the question in my previous comment, now thinking of it. More specific
uchar img_h=*(img_hsv.data+i*img_hsv.step+j*img_hsv.elemSize());
gives you the pixel at (i,j), while (i,j) is initialized to (0,0). Therefore, you get an entirely black image, if the first pixel at (0,0) falls below your threshold, and a white image otherwise. You probably rather want to recalculate img_h, img_s and img_v in each iteration.
In pre-cv2.x there was a macro to access pixels as CV_IMAGE_ELEM, please read the manual for a replacement. Shouldn't be there something like img_hsv(i,j) now?
Upvotes: 1