Ask Your Question
1

How to improve thresholding

asked 2015-10-18 18:42:54 -0600

vitruvius gravatar image

Hello,

I have an image and I would like apply threshold operation and detect the coin in my image. Right now I'm using Otsu's method, but even with different thresholding values and smoothing types, I couldn't get any clean output.

Is there any way that I can improve this outcome?

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    namedWindow("Threshold", CV_WINDOW_AUTOSIZE);

    /// Load the source image
    Mat src;
    src = imread("1cent.png", 1); 

    Mat src_gray;
    cvtColor(src, src_gray, CV_RGB2GRAY);

    Mat smooth;
    for (int i = 1; i < 32; i = i + 2)
    {
        GaussianBlur(src_gray, smooth, Size(i, i), 0, 0);
    }   

    Mat thresh;
    threshold(smooth, thresh, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);

    imshow("Threshold", thresh);

    waitKey(0);
    return 0;
}

Source image:

Source image

Threshold output: Threshold output

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-10-19 00:51:03 -0600

updated 2015-10-19 00:51:31 -0600

Hi @vitruvius

Since the color of Coin & the background are quite distinct,the correct solution is to simply switch to different color space(i.e HSV color space )!

Mat mSource_Bgr,mSource_Hsv,mMask;
mSource_Bgr= imread(FileName_S,1);
imshow("Source Image",mSource_Bgr);
cvtColor(mSource_Bgr,mSource_Hsv,COLOR_BGR2HSV);
inRange(mSource_Hsv,Scalar(0,55,0),Scalar(30,255,255),mMask);

imshow("Mask Image",mMask);

image description

edit flag offensive delete link more

Comments

Thanks for your solution. Would changing the color space cause me any problems for other objects? Or should I always do this as a first step? My application is about detecting objects.

vitruvius gravatar imagevitruvius ( 2015-10-19 04:08:12 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-10-18 18:42:54 -0600

Seen: 716 times

Last updated: Oct 19 '15