簡體   English   中英

如何在matlab中的灰色二進制圖像內繪制矩形

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

我是matlab的新手。 我正在一篇有關手寫文本文檔分割的論文。 在這種情況下,我需要在灰色的二進制圖像內繪制一個彩色矩形並顯示它。 我用過ShapeInserter 這是我的代碼。

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);

但是我得到這個錯誤。

使用images.internal.imageDisplayValidateParams> validateCData(第119行)時出錯>如果輸入是邏輯(二進制),則它必須是二維的。

images.internal.imageDisplayValidateParams(第27行)中的錯誤common_args.CData = validateCData(common_args.CData,image_type);

images.internal.imageDisplayParseInputs(第78行)中的錯誤common_args = images.internal.imageDisplayValidateParams(common_args);

輸入錯誤(第227行)
[common_args,specific_args] = ...

draw_rectangle_inside_image中的錯誤(第13行)
Figure('name','Edited Image'),imshow(img1);

如果我將此行更改為“ rgb = repmat(binary_image,[1,1,3]); ”改為此“ rgb = ind2rgb(binary_image,[1,1,3]); “我得到一個紅色矩形,但沒有我想將紅色矩形放在哪里的圖像,我已經嘗試解決了近4天的問題,但失敗了。 幫助我盡快使用適當的經過測試的解決方案解決此問題。

專門查看錯誤的第一部分:

Error using images.internal.imageDisplayValidateParams>validateCData(line 119) >If input is logical (binary), it must be two-dimensional.

shapeInserter期望輸入是2D 二進制圖像。 但是,由於您的repmat調用,您的圖像改為是3D二進制圖像。 如果圖像是3D(多通道),則必須uint8/uint16/uint32類型,而不是logical /二進制。

實際上,您不需要復制通道即可使用shapeInserter將形狀繪制到圖像上。 您可以簡單地使用二進制映像代替。 但是,由於我最終不知道將要使用哪個應用程序,因此我假設您需要二進制圖像的RGB表示形式。

這樣,在將映像轉換為二進制文件之后,立即將其轉換回uint8

只需使用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);

在股票圖像( cameraman.tif )上運行您的代碼,加上我對您的代碼所做的一點補充,這就是我得到的:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM