简体   繁体   English

如何在matlab中的灰色二进制图像内绘制矩形

[英]how to draw rectangle inside a gray binary image in matlab

I am new in matlab . 我是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 . 我用过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. 使用images.internal.imageDisplayValidateParams> validateCData(第119行)时出错>如果输入是逻辑(二进制),则它必须是二维的。

Error in images.internal.imageDisplayValidateParams (line 27) common_args.CData = validateCData(common_args.CData,image_type); images.internal.imageDisplayValidateParams(第27行)中的错误common_args.CData = validateCData(common_args.CData,image_type);

Error in images.internal.imageDisplayParseInputs (line 78) common_args = images.internal.imageDisplayValidateParams(common_args); images.internal.imageDisplayParseInputs(第78行)中的错误common_args = images.internal.imageDisplayValidateParams(common_args);

Error in imshow (line 227) 输入错误(第227行)
[common_args,specific_args] = ... [common_args,specific_args] = ...

Error in draw_rectangle_inside_image (line 13) draw_rectangle_inside_image中的错误(第13行)
figure('name','Edited Image'),imshow(img1); 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. 如果我将此行更改为“ rgb = repmat(binary_image,[1,1,3]); ”改为此“ rgb = ind2rgb(binary_image,[1,1,3]); “我得到一个红色矩形,但没有我想将红色矩形放在哪里的图像,我已经尝试解决了近4天的问题,但失败了。 Help me to resolve this problem with a proper tested solution as soon as possible. 帮助我尽快使用适当的经过测试的解决方案解决此问题。

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. shapeInserter期望输入是2D 二进制图像。 However, because of your repmat call, your image is a 3D binary image instead. 但是,由于您的repmat调用,您的图像改为是3D二进制图像。 If the image is 3D (multi-channel), it must be of type uint8/uint16/uint32 , not logical / binary. 如果图像是3D(多通道),则必须uint8/uint16/uint32类型,而不是logical /二进制。

You actually don't need to replicate the channels to draw a shape onto an image with shapeInserter . 实际上,您不需要复制通道即可使用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. 但是,由于我最终不知道将要使用哪个应用程序,因此我假设您需要二进制图像的RGB表示形式。

As such, right after you convert your image into binary, convert it back to uint8 . 这样,在将映像转换为二进制文件之后,立即将其转换回uint8

Simply change the type of the image back to uint8 with im2uint8 : 只需使用im2uint8将图像的类型更改回uint8

%// 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: 在股票图像( cameraman.tif )上运行您的代码,加上我对您的代码所做的一点补充,这就是我得到的:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM