Prev: Minimizing an Integral function
Next: image matrix
From: Zanie on 12 Feb 2010 17:48 the code is running: %% Character Recognition clear all ; clear all; %% Read Image img = imread('pic.bmp'); %% Gray Scale if size(img,3)==3 %RGB image imagen=rgb2gray(img); end threshold = graythresh(imagen); imagen =~im2bw(imagen,threshold); re=imagen; %% Morphology % % * *Image Dilation* se = strel('square', 2); Iedge2 = imdilate(re, se); %% * *Image Filling* Ifill= imfill(Iedge2,'holes'); %% Blobs analysis [Ilabel num] = bwlabel(Iedge2); Iprops = regionprops(Ilabel); Ibox = [Iprops.BoundingBox]; Ibox = reshape(Ibox,[4, num]); subplot(2,1,1); imshow(img); %% Plot the Object Location hold on; %% try for cnt = 1:num lig= rectangle('position',Ibox(:,cnt),'edgecolor','g'); disp('this is :');disp(cnt);disp(Ibox(:,cnt)); % rec1[x,y,w,h]=Ibox(:,cnt); % cur=imcrop(img,rec); cur= imcrop(img,Ibox(:,cnt)); subplot(2,1,2); imshow(cur); pause(0.5); end the image is : (http://www.flickr.com/photos/47557774(a)N08/4351514461/) but i want to do something like below: prev=[ 1,1,1,1]; maxx=2; maxy=2; for cnt = 1:num lig= rectangle('position',Ibox(:,cnt),'edgecolor','g'); cur= imcrop(img,Ibox(:,cnt)); cur.maxx=minx+width; cur.maxy=miny+height; if(prev.minx>cur.minx) & (prev.miny<cur.miny) & (prev.maxx<cur.maxx) & (prev.maxy<cur.maxy) newRect=rectangle('position',[cur.minx,prev.miny,cur.width,maxy-miny],'edgecolor','g'); current=imcrop(newRect); subplot(5,1,4); imshow(current); pause(0.5); end when you run the programme it show you the two part of 'i'. I want to make it 1. this code is not valid in matlab but there would be some alternative code. Did you get my point???
From: ImageAnalyst on 12 Feb 2010 21:18 Zanie: You have to make some kind of criteria for joining separate blobs into one letter. For example you can say that if their mean x's are similar, and their bounding boxes are close enough in the vertical direction, then you should combine the bounding boxes into a new, larger one. I do that in this well commented example: (Be sure to join any lines broken by the newsreader into two lines) fullFileName = 'pic.bmp'; originalImage = imread(fullFileName); [rows columns numberOfColorBands] = size(originalImage); if numberOfColorBands > 1 % Convert to monochrome if necessary. originalImage = originalImage(:,:,1); end subplot(3, 3, 1); imagesc(originalImage); colormap(gray(256)); caption = sprintf('Original "coins" image showing\n6 nickels (the larger coins) and 4 dimes (the smaller coins).'); title(caption); axis square; % Make sure image is not artificially stretched because of screen's aspect ratio. % Maximize the figure window. set(gcf, 'Position', get(0, 'ScreenSize')); % Just for fun, let's get its histogram. [pixelCount grayLevels] = imhist(originalImage); subplot(3, 3, 2); bar(pixelCount); title('Histogram of original image'); xlim([0 grayLevels(end)]); % Scale x axis manually. % Threshold the image to get a binary image (only 0's and 1's) of class "logical." % Method #1: using im2bw() % normalizedThresholdValue = 0.4; % In range 0 to 1. % thresholdValue = normalizedThresholdValue * max(max(originalImage)); % Gray Levels. % binaryImage = im2bw(originalImage, normalizedThresholdValue); % One way to threshold to binary % Method #2: using a logical operation. thresholdValue = 250; % binaryImage = originalImage > thresholdValue; % Bright objects will be the chosen if you use >. binaryImage = originalImage < thresholdValue; % Dark objects will be the chosen if you use <. % Do a "hole fill" to get rid of any background pixels inside the blobs. binaryImage = imfill(binaryImage, 'holes'); % Show the threshold as a vertical red bar on the histogram. hold on; maxYValue = ylim; hStemLines = stem(thresholdValue, maxYValue(2), 'r'); children = get(hStemLines, 'children'); set(children(2),'visible', 'off'); % Place a text label on the bar chart showing the threshold. annotationText = sprintf('Thresholded at %d gray levels', thresholdValue); % For text(), the x and y need to be of the data class "double" so let's cast both to double. text(double(thresholdValue + 5), double(0.5 * maxYValue(2)), annotationText, 'FontSize', 10, 'Color', [0 .5 0]); text(double(thresholdValue - 70), double(0.94 * maxYValue(2)), 'Background', 'FontSize', 10, 'Color', [0 0 .5]); text(double(thresholdValue + 50), double(0.94 * maxYValue(2)), 'Foreground', 'FontSize', 10, 'Color', [0 0 .5]); % Display the binary image. subplot(3, 3, 3); imagesc(binaryImage); colormap(gray(256)); title('Binary Image, obtained by thresholding'); axis square; labeledImage = bwlabel(binaryImage, 8); % Label each blob so we can make measurements of it coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels subplot(3, 3, 4); imagesc(labeledImage); title('Labeled Image, from bwlabel()'); axis square; subplot(3, 3, 5); imagesc(coloredLabels); title('Pseudo colored labels, from label2rgb()'); axis square; % Get all the blob properties. Can only pass in originalImage in version R2008a and later. blobMeasurements = regionprops(labeledImage, originalImage, 'all'); numberOfBlobs = size(blobMeasurements, 1); blobsToIgnore = []; letterBoundingBox = zeros(4, numberOfBlobs); % Go through all the blobs, getting their bounding boxes, % and combining their bounding boxes with the bounding boxes % of nearby ones if they're close enough. for k = 1 : numberOfBlobs % Loop through all blobs. if ismember(k, blobsToIgnore) % If blob has been combined with another blob % already, skip it. continue; end % First just assume bounding box is from this one single blob. blobBoundingBox = blobMeasurements(k).BoundingBox; % Get BoundingBox. x1k = blobBoundingBox(1); y1k = blobBoundingBox(2); x2k = x1k + blobBoundingBox(3); y2k = y1k + blobBoundingBox(4); avexk = mean([x1k x2k]); % Now see if any other blob is close to it. for kk = (k+1):numberOfBlobs blobBoundingBox2 = blobMeasurements(kk).BoundingBox; % Get BoundingBox. % Get bounding x1, x2, y1, y2. x1kk = blobBoundingBox2(1); y1kk = blobBoundingBox2(2); x2kk = x1kk + blobBoundingBox2(3); y2kk = y1kk + blobBoundingBox2(4); avexkk = mean([x1kk x2kk]); deltaX = abs(avexk-avexkk); % Check for closeness in the Y direction. % They need to be within 10 in they direction % and within 5 in the x direction. if (abs(y1kk-y2k) < 10 || abs(y2kk-y1k) < 10) && ... deltaX < 5 % Then two blobs are so close that their % bounding boxes should be joined. x1k = min([x1k, x1kk]); y1k = min([y1k, y1kk]); x2k = max([x2k, x2kk]); y2k = max([y2k, y2kk]); blobsToIgnore = [blobsToIgnore kk]; end end % Save the x and y's for the single blob. % We'll modify it later if we need to combine blobs. letterBoundingBox(1:4, k) = [x1k y1k x2k y2k]; end % Now let's crop out the letters. plotNumber = 6; letterNumber = 1; for k = 1 : numberOfBlobs % Loop through all blobs. if letterBoundingBox(1, k) ~= 0 % It's a valid bounding box. % crop the image. % Get the width and height, for cropping. width = abs(letterBoundingBox(3, k) - letterBoundingBox(1, k))+1; height = abs(letterBoundingBox(4, k) - letterBoundingBox(2, k))+1; % Construct the rectangle. r = [letterBoundingBox(1, k), letterBoundingBox(2, k), ... width, height]; % Crop out just this letter. croppedLetter = imcrop(binaryImage, r); % Display the letter. subplot(3, 3, plotNumber); imshow(croppedLetter, []); caption = sprintf('Letter #%d', letterNumber); title(caption); % Increment the plot number for next time. plotNumber = plotNumber + 1; % Increment the letter number for next caption. letterNumber = letterNumber + 1; end end msgbox('Done');
From: Zanie on 13 Feb 2010 02:52
Thanx a lot it helped me soo much |