OpenCV (C++) - Set HSV values of a pixel

后端 未结 1 1372
攒了一身酷
攒了一身酷 2021-01-24 16:15

I have an RGB image that i converted to HSV and my goal is to set every pixel that doesn\'t meet a certain hue value (100) to black. So H = S = V = 0.

I have this code:

相关标签:
1条回答
  • 2021-01-24 16:54

    You can:

    • build a mask where H channel equals 100, regardless the values of S,V channels (with inRange)
    • set frame3 to zero according to the mask (with setTo)

    Something like:

    Mat frame3; // CV_8UC3, HSV image
    
    Mat mask;
    inRange(frame3, Scalar(100,0,0), Scalar(100, 255, 255), mask);
    
    frame3.setTo(Scalar(0,0,0), mask);
    

    To keep your code structure, you need to modify the actual value, not a copy of the value. You can do that keeping a reference to the value:

     Vec3b& hsv = frame3.at<Vec3b>(i, j);
     if (hsv[0] != hue) {
        hsv[0] = 0;
        hsv[1] = 0;
        hsv[2] = 0;
     }  
    
    0 讨论(0)
提交回复
热议问题