Clear Filters
Clear Filters

How to get rid of the error: Error using horzcat. Dimensions of matrices being concatenated are not consistent.

3 views (last 30 days)
I'm segmenting line from a document image using the following code. I want to store the segmented line in a folder from where i could use those individual images for recognition.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Read in a standard MATLAB gray scale demo image.
folder = 'F:\SSN\project\Phase II\6th Review\Whole image\seg';
baseFileName = 'image_a.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
pixelCount(end) = 0; % Suppress spike so we can see shape.
subplot(2, 2, 2);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
verticalProfile = mean(grayImage, 2);
subplot(2, 2, 3);
plot(verticalProfile);
% Find the white spaces
whiteGaps = verticalProfile > 250;
% Get the centroids of the white gaps
measurements = regionprops(whiteGaps, 'Centroid');
allCentroids = [measurements.Centroid];
centroidX = allCentroids(1:2:end);
centroidY = allCentroids(2:2:end);
% Now we have the lines to crop out.
% Make a new figure
binaryImage = grayImage < 125;
for k = 1 : length(centroidY)-1
line1 = int32(centroidY(k));
line2 = int32(centroidY(k+1));
thisLine = binaryImage(line1:line2, :);
thisLine=imresize(thisLine,[100 100]);
imwrite(thisLine,['Datasets/',num2str(thisLine),'.jpg']);
figure,
imshow(thisLine);
end

Accepted Answer

Jos (10584)
Jos (10584) on 22 Apr 2016
This is not going well:
thisLine=imresize(thisLine,[100 100]);
imwrite(thisLine,['Datasets/',num2str(thisLine),'.jpg']);
In the first line you create a 100-by-100 matrix with numbers. In the second line you convert this array to a string using num2str, apparently because you want a filename ...
thisLine = imresize(thisLine,[100 100]);
XXX = ... % what should be here?
filename = ['Datasets/' XXX '.jpg']
imwrite(thisLine, filename);

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!