Converting an OpenCV Image to Black and White

后端 未结 7 1528
逝去的感伤
逝去的感伤 2020-11-29 17:38

How do you convert a grayscale OpenCV image to black and white? I see a similar question has already been asked, but I\'m using OpenCV 2.3, and the proposed solution no long

7条回答
  •  温柔的废话
    2020-11-29 18:05

    Specifying CV_THRESH_OTSU causes the threshold value to be ignored. From the documentation:

    Also, the special value THRESH_OTSU may be combined with one of the above values. In this case, the function determines the optimal threshold value using the Otsu’s algorithm and uses it instead of the specified thresh . The function returns the computed threshold value. Currently, the Otsu’s method is implemented only for 8-bit images.

    This code reads frames from the camera and performs the binary threshold at the value 20.

    #include "opencv2/core/core.hpp"
    #include "opencv2/imgproc/imgproc.hpp"
    #include "opencv2/highgui/highgui.hpp"
    
    using namespace cv;
    
    int main(int argc, const char * argv[]) {
    
        VideoCapture cap; 
        if(argc > 1) 
            cap.open(string(argv[1])); 
        else 
            cap.open(0); 
        Mat frame; 
        namedWindow("video", 1); 
        for(;;) {
            cap >> frame; 
            if(!frame.data) 
                break; 
            cvtColor(frame, frame, CV_BGR2GRAY);
            threshold(frame, frame, 20, 255, THRESH_BINARY);
            imshow("video", frame); 
            if(waitKey(30) >= 0) 
                break;
        }
    
        return 0;
    }
    

提交回复
热议问题