How to find the location of red region in first image and mark the same location in grayscale image using matlab?
-
Well, i get the result of gradient as a graylevel image, not color image as shown in first image. I think it is just a color representation of illumination level of the image. In that case, you need to threshold the image for suitable value to isolate regions of high illumintation. I have edited my answer for that.– Abid Rahman KJan 26, 2012 at 14:04
-
@Kim Just curious: How was the first image generated?– John M.Oct 6, 2017 at 16:24
-
IR camera / thermal Imaging @JohnM.– Mah NehFeb 16 at 19:00
1 Answer
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.
-
how can I continue with this coding?? Last part of my coding is this... figure(),ColourBar = imagesc(C); colorbar;– KimJan 26, 2012 at 6:41
-
Is the output of this code same as first image in your question? I mean, high red color in the location of barcode? Jan 26, 2012 at 6:44
-
-
I know. I have seen that question. If that is so, the procedure i have told above is simple to implement. First split resulting image to R plane as i told above. Apply threshold. Commands also i have given above. Can't you try? First of all, i again suggest you to read the book i mentioned. Without basic knowledge, you cannot go forward. Jan 26, 2012 at 6:58
-
I did try the coding but it won't work... Attempted to access ColourBar(:,2); index out of bounds because numel(ColourBar)=1. Is it I have the wrong variable??– KimJan 26, 2012 at 7:08