且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用MATLAB查找图像中红色区域的位置?

更新时间:2023-02-26 14:36:19

希望这是。


How to find the location of red region in first image and mark the same location in grayscale image using matlab?

Hope this is continuation of this question.

I would like to suggest you that first read some good and fundamental books on image processing. I recommend this book: Digital image processing using MATLAB by Gonzalez.

Anyway regarding your question:

1) Convert image to R,G,B plane.

Image_red = Image_rgb(:,1);
Image_green = Image_rgb(:,2);
Image_blue = Image_rgb(:,3);

2) Since required region has high content of red(as per the image in your question), take R plane and threshold for suitable value.

BW = im2bw(Image_red, threshold value)

3) You get binary image where barcode region is one color(suppose white) and other part is a different color (black).

4) Now get the location of this white region as a minimum bounding rectangle.

STATS = regionprops(BW, 'BoundingBox')

Once you got this location of as rectangle, do whatever you like. For example, to isolate the barcode for barcode recognition, crop this rectangle from original image using imcrop command:

barcode = imcrop(original_image, rect)

(I know the code is not complete. I gave only hints of commands to use. Because, i am not familiar with matlab and i use opencv for image processing. But i am sure, it is an easy task and you can complete the code).

EDIT:

After i implemented the derivation formula and apply low pass filtering, image i got is a grayscale. I just applied a threshold so that i get only regions of high illumination(which include barcode region) and blackout all other portion. Now apply some erosion to remove simple noises or small false detection. Apply dilation for compensation. Now find contour with maximum area (which is, most probably, the barcode). Get smallest possible bounding rectangle for it. It is your barcode. ( I just implemented it in OpenCV Python. I don't know how to do it in Matlab). Below is some test results:

Below is the OpenCV code:

#### Code for BARCODE detection  ######
import cv,sys
imgco = cv.LoadImage('image.jpg')
img = cv.CreateImage(cv.GetSize(imgco),8,1)
imgx = cv.CreateImage(cv.GetSize(img),cv.IPL_DEPTH_16S,1)
imgy = cv.CreateImage(cv.GetSize(img),cv.IPL_DEPTH_16S,1)
thresh = cv.CreateImage(cv.GetSize(img),8,1)

### Convert image to grayscale ###
cv.CvtColor(imgco,img,cv.CV_BGR2GRAY)

### Finding horizontal and vertical gradient ###

cv.Sobel(img,imgx,1,0,3)
cv.Abs(imgx,imgx)

cv.Sobel(img,imgy,0,1,3)
cv.Abs(imgy,imgy)

cv.Sub(imgx,imgy,imgx)
cv.ConvertScale(imgx,img)

### Low pass filtering ###
cv.Smooth(img,img,cv.CV_GAUSSIAN,7,7,0)

### Applying Threshold ###
cv.Threshold(img,thresh,100,255,cv.CV_THRESH_BINARY)

cv.Erode(thresh,thresh,None,2)
cv.Dilate(thresh,thresh,None,5)

### Contour finding with max. area ###
storage = cv.CreateMemStorage(0)
contour = cv.FindContours(thresh, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)
area = 0
while contour:
    max_area = cv.ContourArea(contour)
    if max_area>area:
        area = max_area
        bar = list(contour) 
    contour=contour.h_next()

### Draw bounding rectangles ###
bound_rect = cv.BoundingRect(bar)
pt1 = (bound_rect[0], bound_rect[1])
pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
cv.Rectangle(imgco, pt1, pt2, cv.CV_RGB(0,255,255), 2)

cv.ShowImage('img',imgco)    
cv.WaitKey(0)   

Try this code. If you understand the code, try to convert to matlab. You can see OpenCV documentation here.