views:

237

answers:

1

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!!!

+1  A: 

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?

zerm
ok... once again thanks!!! i should admit that i am a neophyte.... so such mistakes are prone to happen... !!! Hey... zerm... i am actually doing a project on image processing ... can you send ur mail id to [email protected] so dat i can ask you doubts personally?( only if you wish..)
Kaushal
you should rather ask here, so other can help too. I do SO just in my breaks, otherwise i'm a pretty busy person ;)
zerm
i see.... alright...!!
Kaushal