Jack welch
Jack welch

Reputation: 1707

Detecting if a bottle has a label

enter image description hereenter image description hereenter image description hereI am currently doing a bit of computer vision using openCv. I have a sample of bottles a label on it. I am trying to determine when a bottle does not have a label on it. The label is rectangular in shape.

I have done an edge detection using Canny.I have tried using findcountour() to detect if a bottle has an inner contour(this would represent the rectangular label).

Upvotes: 2

Views: 2940

Answers (4)

samkhan13
samkhan13

Reputation: 3385

guneykayim suggested image segmentation which I feel would be the easiest method. I am just adding a little bit more...

my suggestion is that you convert your BGR image into YCbCr and then look for values within the Cb and Cr channels to match the color of your label. This will allow you to easily segment out colors even if lighting conditions on the bottle change (a darkly lit bottle will end up having white regions appear dark gray and this can be a problem if you have gray colored labeling)

something like this should work in python:

# Required moduls
import cv2
import numpy

# Convert image to YCrCb
imageYCrCb = cv2.cvtColor(sourceImage,cv2.COLOR_BGR2YCR_CB)

# Constants for finding range of label color in YCrCb
# a, b, c and d need to be defined
min_YCrCb = numpy.array([0,a,b],numpy.uint8) 
max_YCrCb = numpy.array([0,c,d],numpy.uint8)

# Threshold the image to produce blobs that indicate the labeling
labelRegion = cv2.inRange(imageYCrCb,min_YCrCb,max_YCrCb)

# Just in case you are interested in going an extra step
contours, hierarchy = cv2.findContours(labelRegion, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Draw the contour on the source image
for i, c in enumerate(contours):
    area = cv2.contourArea(c)
    if area > minArea: # minArea needs to be defined, try 300 square pixels
        cv2.drawContours(sourceImage, contours, i, (0, 255, 0), 3)

the function cv2.inRange() will also work incase you decided to work with BGR image.

Reference:

http://en.wikipedia.org/wiki/YCbCr

Upvotes: 1

Zaw Lin
Zaw Lin

Reputation: 5708

#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;
int main()
{

    Mat img=imread("c:/data/bottles/1.png");
    Mat gray;
    cvtColor(img,gray,CV_BGR2GRAY);
    resize(gray,gray,Size(50,100));
    Sobel(gray,gray,CV_16SC1,0,1);
    convertScaleAbs(gray,gray);
    if(sum(gray)[0]<130000)
    {       
        cout<<"no label";
    }else{
        cout<<"has label";
    }
    imshow("gray",gray);
    waitKey();
    return 0;
}

Upvotes: 2

guneykayim
guneykayim

Reputation: 5250

If your problem is this simple, just place reduce your image using a rectangle.

cv::Mat image = imread("image.png");
cv::Rect labelRegion(50, 200, 50, 50);
cv::Mat labelImage = image(labelRegion);

Then decompose your image region into three channels.

cv::Mat channels[3];
cv::split(labelImage, channels);

cv::Mat labelImageRed = channels[2];
cv::Mat labelImageGreen = channels[1];
cv::Mat labelImageBlue = channels[0];

Then threshold each of these one channeled images and count number of zero/nonzero pixels.

I'm not providing code for this part!

If you don't have label on the image then each channel has values bigger then ~200 (you should check this). If there is a label, then you will see different result when counting zero/nonzero pixels from the non labeled one.

Upvotes: 5

scap3y
scap3y

Reputation: 1198

I am guessing it should be enough to just see if there is text present on the bottle or not (if yes, then it has a label and vice versa).. You could check out a project like THIS.. There are numerous papers in this area; some of the more famous ones are done by the Stanford CV group - 1 and 2..

HTH

Upvotes: 1

Related Questions