OpenCV 图像从 RGB 到 HSV 阈值转换的难题

发布于 2024-09-05 13:23:38 字数 1343 浏览 3 评论 0原文

这是我的代码。当我执行它的时候。给定任何仲裁图像。它总是给出全黑或全白图像的结果。迭代有问题吗?

    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);

谢谢提前回复!!!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚如初 2024-09-12 13:23:38

我想我已经在之前的评论中回答了这个问题,现在正在考虑。
更具体地说,

uchar img_h=*(img_hsv.data+i*img_hsv.step+j*img_hsv.elemSize());

给出 (i,j) 处的像素,而 (i,j) 初始化为 (0,0)。因此,如果 (0,0) 处的第一个像素低于阈值,您将获得全黑图像,否则将获得白色图像。
您可能更想在每次迭代中重新计算 img_h、img_s 和 img_v。

在 cv2.x 之前的版本中,有一个用于访问像素的宏 CV_IMAGE_ELEM,请阅读手册进行替换。现在不应该有类似 img_hsv(i,j) 的东西吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文