How to find the location of the red area in an image using MATLAB?

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

Colored Image

Marked image

+3
source share
1 answer

I hope this is a continuation of this issue.

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

In any case, regarding your question:

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

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

2) Since the required area has a high red content (according to the image in your question), take the R-plane and the threshold for a suitable value.

 BW = im2bw(Image_red, threshold value) 

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

4) Now find the location of this white area as the minimum bounding box.

 STATS = regionprops(BW, 'BoundingBox') 

Once you get this location as a rectangle, do whatever you want. For example, to isolate a barcode for barcode recognition, crop this rectangle from the original image with the imcrop command:

 barcode = imcrop(original_image, rect) 

(I know that the code is not complete. I gave only hints for using commands. Because I am not familiar with Matlab and I use opencv for image processing. But I am sure that this is a simple task and you can fill in the code).

EDIT:

After I applied the derivation formula and applied low-pass filtering, the resulting image got shades of gray. I simply applied the threshold to get only high-light areas (including the barcode area) and darken all the other parts. Now apply some erosion to remove simple noises or slight false detection. Apply dilatation to compensate. Now find the outline with the maximum area (which is most likely a barcode). Take the smallest possible bounding box for it. This is your barcode. (I just implemented it in OpenCV Python. I don't know how to do this in Matlab). Below are some test results:

Image 1Image 2

The following 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 converting it to matlab. Here you can see the OpenCV documentation.

+28
source

Source: https://habr.com/ru/post/1393877/


All Articles