How to get value of specific pixel after converting to hsv?

后端 未结 2 638
渐次进展
渐次进展 2021-01-03 02:11

I have created a small program to detect red color by converting image to hsv. For detecting red color I am using the min range 170,160,160 and max range 180,255,255. So I g

相关标签:
2条回答
  • 2021-01-03 02:47

    @user3352710 @Pe Dro Hum it's really easy to do that on python. Here is the simple code for it.

    def cor_mouse_disp(event,x,y,flags,param):
        if event == cv2.EVENT_LBUTTONDBLCLK: #left mouse double click 
             print("Orginal BGR:",img[x,y])
             print("HSV values:", HSV[x,y])
    
    path = "your_image_path.png"
    while True:
        img = cv2.imread(path)
        #img = cv2.resize(img, None, fx=0.5, fy=0.5)
        HSV = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    
        cv2.imshow("Original",img)
        cv2.imshow("HSV", HSV)
        #left mouse click event
        cv2.setMouseCallback("HSV", coords_mouse_disp)
        cv2.setMouseCallback("Original", coords_mouse_disp)
    
        if cv2.waitKey(1) &0xFF == ord("q"):
            cv2.destroyAllWindows()
            break
    
    0 讨论(0)
  • 2021-01-03 02:51

    So you already have x and y,

    -> Just create ROI Mat with 1X1 size

    -> Convert HSV colour space using CvtColor()

    -> Access pixel value of above result

    Mat image; //load image here
    Mat HSV;
    Mat RGB=image(Rect(x,y,1,1)); // use your x and y value
    
    cvtColor(RGB, HSV,CV_BGR2HSV);
    Vec3b hsv=HSV.at<Vec3b>(0,0);
    int H=hsv.val[0]; //hue
    int S=hsv.val[1]; //saturation
    int V=hsv.val[2]; //value
    

    You can refer below C++ code which will display RGB and HSV value for the pixel corresponding mouse position.

    char window_name[30] = "HSV Segmentation";
    Mat src;
    
    static void onMouse( int event, int x, int y, int f, void* ){
     Mat image=src.clone();
     Vec3b rgb=image.at<Vec3b>(y,x);
     int B=rgb.val[0];
     int G=rgb.val[1];
     int R=rgb.val[2];
    
      Mat HSV;
      Mat RGB=image(Rect(x,y,1,1));
      cvtColor(RGB, HSV,CV_BGR2HSV);
    
        Vec3b hsv=HSV.at<Vec3b>(0,0);
        int H=hsv.val[0];
        int S=hsv.val[1];
        int V=hsv.val[2];
    
        char name[30];
        sprintf(name,"B=%d",B);
        putText(image,name, Point(150,40) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );
    
        sprintf(name,"G=%d",G);
        putText(image,name, Point(150,80) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );
    
        sprintf(name,"R=%d",R);
        putText(image,name, Point(150,120) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );
    
        sprintf(name,"H=%d",H);
        putText(image,name, Point(25,40) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );
    
        sprintf(name,"S=%d",S);
        putText(image,name, Point(25,80) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );
    
        sprintf(name,"V=%d",V);
        putText(image,name, Point(25,120) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );
    
        sprintf(name,"X=%d",x);
        putText(image,name, Point(25,300) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,0,255), 2,8,false );
    
        sprintf(name,"Y=%d",y);
        putText(image,name, Point(25,340) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,0,255), 2,8,false );
    
     //imwrite("hsv.jpg",image);
     imshow( window_name, image );
    }
    
    
    
    int main(){
     src = imread("bgr.png",1);
     imshow(window_name,src);
     setMouseCallback( window_name, onMouse, 0 );
     waitKey();  
     }
    

    enter image description here

    0 讨论(0)
提交回复
热议问题