1

I am developing an app for iOS with openCV that take a picture from a monitor and extract a curve, but when the image has some bright zones after thresholding, I don't get the complete curve but some black zones

Original image

processed image after thresholding

        original = [MAOpenCV cvMatGrayFromUIImage:_sourceImage];   
        cv::threshold(original, original, 70, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);

        findContours(original, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );

        cv::Mat skel(original.size(), CV_8UC1, cv::Scalar(0));

        int idx = 0;
        for(; idx >= 0; idx = hierarchy[idx][0])
        {
            if (contours[idx].size()>250 && idx>-1){
                cv::Scalar color( 255,255,255);
                drawContours(skel, contours, idx, color, CV_FILLED, 8, hierarchy);
            }
        }
        cv::threshold(skel, skel, 100, 255, CV_THRESH_BINARY_INV);
        cv::erode(skel,skel,cv::Mat(),cv::Point(-1,-1),2);

So how I can process the image to extract the curve when the image have some bright zones like the example

2 Answers2

0

When you have a background with an uneven illumination, you may want to apply first a White Top-Hat (or here for MatLab, and here for OpenCV).

Here is the result I got using a structuring element of type disk with radius 3.

Then, whatever thresholding method you choose will work.

FiReTiTi
  • 5,597
  • 12
  • 30
  • 58
0

Wouldn't be sufficient to use Otsu's thresholding? Otsu's thresholding result

Code fragment:

import cv2

image = cv2.imread('d:/so.jpg', cv2.IMREAD_GRAYSCALE)

threshold, thresholded = cv2.threshold(image, 0, 255, type=cv2.THRESH_BINARY + cv2.THRESH_OTSU)
print(threshold)

cv2.imshow('so', image);
cv2.imshow('thresholded', thresholded)
cv2.waitKey(0)
Pavel Mach
  • 36
  • 4