error while drawing several x-marks on a binary image in matlab

182 Views Asked by At

I am trying to draw several x-marks on a binary image to some co-ordinates. My output should look like this

enter image description here

To do this i have written some codes below

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

But i am getting these errors

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 test2 (line 39)

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

I have tried some examples but they are experimented on RGB image while i need it on binary image and that's where i am getting errors.

Why i am getting these errors and what will be the solution?

Please provide explanation with proper code.

1

There are 1 best solutions below

4
On BEST ANSWER

You code seems a bit complicated (to me) for what it is intended. Here is a simple example demonstrating how to draw red crosses using scatter on a binary image. As you see, the image in itself is binary and the markers are in color, so the 'net' image is no longer binary... Also, there is no need to convert your RGB image to graylevels before applying im2bw.

And don't forget to use hold on to avoid discarding the image when adding the markers!

Here is the code with comments:

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

With output:

enter image description here

Is this what you meant?