Reputation: 327
I am new in matlab. I am doing a thesis on handwritten text document segmentation. In this i need to draw a colorful rectangle inside a gray binary image and display it. I have used ShapeInserter. Here is my code.
file_name = 'test4.jpg';
image = imread(strcat('E:\ARIF\Studying Related\L-4 T-1\CSE-400(Project and Thesis)\Matlab Program\images\',file_name));
figure('name','Main Image'),imshow(image);
gray_image=rgb2gray(image);
level = graythresh(gray_image);
%display(level);
binary_image = im2bw(image,level);
shapeInserter = vision.ShapeInserter('Shape','Rectangles','BorderColor','Custom', 'CustomBorderColor', uint8([255 0 0]));
rect = int16([10, 10, 100, 100]);
rgb = repmat(binary_image, [1, 1, 3]);
img1 = step(shapeInserter, rgb, rect);
figure('name','Edited Image'),imshow(img1);
But i am getting this error.
Error using images.internal.imageDisplayValidateParams>validateCData(line 119) >If input is logical (binary), it must be two-dimensional.
Error in images.internal.imageDisplayValidateParams (line 27) common_args.CData = validateCData(common_args.CData,image_type);
Error in images.internal.imageDisplayParseInputs (line 78) common_args = images.internal.imageDisplayValidateParams(common_args);
Error in imshow (line 227)
[common_args,specific_args] = ...Error in draw_rectangle_inside_image (line 13)
figure('name','Edited Image'),imshow(img1);
If i change this line "rgb = repmat(binary_image, [1, 1, 3]);" to this "rgb = ind2rgb(binary_image, [1, 1, 3]); " i am getting a red rectangle but not the image that where i want to put my red rectangle.I have tried to solve this problem for almost 4 days and failed. Help me to resolve this problem with a proper tested solution as soon as possible.
Upvotes: 1
Views: 2087
Reputation: 104515
Look specifically at the first part of your error:
Error using images.internal.imageDisplayValidateParams>validateCData(line 119) >If input is logical (binary), it must be two-dimensional.
shapeInserter
is expecting that the input is a 2D binary image. However, because of your repmat
call, your image is a 3D binary image instead. If the image is 3D (multi-channel), it must be of type uint8/uint16/uint32
, not logical
/ binary.
You actually don't need to replicate the channels to draw a shape onto an image with shapeInserter
. You can simply use the binary image instead. However, because I don't know what application you're going to be using this for in the end, I'm going to assume that you need the RGB representation of the binary image.
As such, right after you convert your image into binary, convert it back to uint8
.
Simply change the type of the image back to uint8
with im2uint8
:
%// YOUR CODE
file_name = 'test4.jpg';
image = imread(strcat('E:\ARIF\Studying Related\L-4 T-1\CSE-400(Project and Thesis)\Matlab Program\images\',file_name));
figure('name','Main Image'),imshow(image);
gray_image=rgb2gray(image);
level = graythresh(gray_image);
%display(level);
binary_image = im2bw(image,level);
%// NEW CODE
binary_image = im2uint8(binary_image);
%// YOUR CODE FROM BEFORE
shapeInserter = vision.ShapeInserter('Shape','Rectangles','BorderColor','Custom', 'CustomBorderColor', uint8([255 0 0]));
rect = int16([10, 10, 100, 100]);
rgb = repmat(binary_image, [1, 1, 3]);
img1 = step(shapeInserter, rgb, rect);
figure('name','Edited Image'),imshow(img1);
Running your code on a stock image (cameraman.tif
), with the slight addition that I made to your code, this is what I get:
Upvotes: 1