在matlab中

时间:2015-06-09 14:48:25

标签: matlab image-processing

我试图在二进制图像上绘制几个 x-marks 到一些坐标。 我的输出应该如下所示

enter image description here

要做到这一点,我在下面写了一些代码

clear all;

% Here four co-ordinates are provided as an example. In my main code the co-  ordinates will be hundred or thousands 
position{1}.x = 10;
position{1}.y = 10;
position{2}.x = 20;
position{2}.y = 20;
position{3}.x = 30;
position{3}.y = 30;
position{4}.x = 40;
position{4}.y = 40;

% Read image as binary
image = imread('test34.jpg');
figure('name','Main Image'),imshow(image);

gray_image=rgb2gray(image);
level = graythresh(gray_image);
binary_image = im2bw(image,level);

% Define color of the x-marker and initializing shapes which i want to draw
red = uint8([255 0 0]);
markerInserter = vision.MarkerInserter('Shape','X-mark','BorderColor','Custom','CustomBorderColor',red);

i = 1;
% The loop will continue to the number of co-ordinates which will never be predefined in my main code

while i<=numel(position) 
    % Converting binary to RGB for only once. 
    if i == 1
        rgb = repmat(binary_image, [1, 1, 3]);
    else

        rgb = repmat(binary_image, [1, 1, 1]);
    end       
    % Position where x-marks will be drawn

    Pts = int32([position{i}.x position{i}.y]);
    % Draw x-marks  
    binary_image = step(markerInserter, rgb, Pts);

    i = i+1;
end

figure('name','binary_image'),imshow(binary_image);

但我收到了这些错误

  

使用images.internal.imageDisplayValidateParams&gt; 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);

     

imshow中的错误(第227行)

     

[common_args,specific_args] = ...

     

test2中的错误(第39行)

     

图(&#39;名称&#39;&#39; binary_image&#39),imshow(binary_image);

我尝试了一些示例,但是他们在 RGB图像上进行了实验,而我需要在二进制图像上以及我在哪里遇到错误。

为什么我会收到这些错误以及解决方案是什么?

请提供适当代码的说明。

1 个答案:

答案 0 :(得分:2)

你的代码看起来有点复杂(对我而言)。这是一个简单的示例,演示如何在二进制图像上使用scatter绘制红色十字。如您所见,图像本身是二进制的,标记是彩色的,因此“网络”图像不再是二进制...此外,在应用im2bw之前,无需将RGB图像转换为灰度图像

不要忘记使用hold on来避免在添加标记时丢弃图像!

以下是带注释的代码:

clear
clc
close all

%// Read image
MyImage = imread('peppers.png');

%// Convert to binary. No need to use rgb2gray.
MyBWImage = im2bw(MyImage(:,:,2));

%// Define some x- and y positions for markers
xpositions = linspace(10,300,40);
ypositions = linspace(20,500,40);

imshow(MyBWImage)

%// IMPORTANT!!
hold on

%// Draw markers
scatter(xpositions.',ypositions.',80,'r','x')

输出:

enter image description here

这是你的意思吗?