How do I fix the error: In an assignment A(:) = B, the number of elements in A and B must be the same.?

Hello everyone,
I am trying to assign an RGB value to a line drawn from input coordinates in an inputted colour image. When I use my function from the command window to execute the task I get an error. Below is my function code and the line where I get my error. May someone kindly help me fix the error.
Error in Linecol (line 35) imgColour(indices) = [cR cG cB]; %Assigning a colour to the drawn line.
function [imgColour] = Linecol(imgIn, P1, P2, RGB)
%*/LINE PLOTER BETWEEN TWO INPUT COORDINATES IN A COLOUR INPUT IMAGE/*
By Allycan
Part 2
DATE: 04 September 2015
Version A
%}
%______________________________________________________________________
%---------------COORDINATE AND COLOUR REFERENCING----------------------
P1r = P1(1);
P1c = P1(2);
P2r = P2(1);
P2c = P2(2);
cR=RGB(1);
cG=RGB(2);
cB=RGB(3);
%--------------------------INPUT IMAGE---------------------------------
imgColour = imgIn;
%------------------------DRAWING THE LINE------------------------------
rowpoints = linspace(P1r,P2r,2000); %Getting the row values/points that will make up the line.
columnpoints = linspace(P1c,P2c,2000); %Getting the column values/points that will make up the line.
indices = sub2ind(size(imgColour),round(rowpoints),round(columnpoints)); %Converting the row and column values/points to linear indices/(Creation of the line).
imgColour(indices) = [cR cG cB]; %Assigning a colour to the drawn line.
end

 Accepted Answer

Note that if you have the computer vision toolbox there are functions in there to directly draw shapes, including lines, into images. Your algorithm is fairly crude, you're overplotting on the same points many times an I've got a feeling it's going to give you lines of uneven thickness. I would recommend you look at well established line drawing algorithms. Bresenham's is fast and simple to implement.
There are many ways to solve your assignment issue. Notice that the indices you calculate are all in the first colour plane. Therefore you could use these to index into the separated colour planes to assign their respective colour:
indices = sub2ind(... %code unchanged to this point
imgr = imgColour(:, :, 1); imgr(indices) = cR; %assign red plane
imgg = imgColour(:, :, 2); imgg(indices) = cG; %assign green plane
imgb = imgColour(:, :, 3); imgb(indices) = cB; %assign blue plane
imgColour = cat(3, imgr, imgg, imgb);
Or you could just compute the offset to the green and blue plane, and replicate your destination colour to make a matrix to matrix assignment:
indices = sub2ind(... %code unchanged to this point
greenoffset = size(image, 1) * size(image, 2);
blueoffest = 2 * greenoffset;
destcolour = repmat(RGB, numel(indices), 1);
imgColour([indices, indices + greenoffset, indices + blueoffset]) = destcolour(:);

1 Comment

Thank you Guillaume. I appreciate your suggestions. I did not want to use the plot functions. The line I have from my code looks like the image attached which appears to have even thickness. Thank you for making me realise that I was trying to assign colours in just one layer basically what Stephen was saying before. I was able to use Stephen's suggestion as well to get the colour but it was only showing the red plane since the layers were not assigned.
attac

Sign in to comment.

More Answers (1)

You have defined each of rowpoints and columnpoints to have 2000 elements. These values are then converted to linear indices using 2ub2ind, which means the variable indices also has 2000 elements.
The values of cR, cG and cB are all scalars, being defined by cR=RGB(1), cG=RGB(2), and cB=RGB(3). These are then combined into a 1*3 vector using [cR cG cB].
So the line
imgColour(indices) = [cR cG cB];
is trying to fit a vector of three elements into two thousand elements, thus the error. It is not really clear what you intend to achieve with this code, but remember that when allocating values using indexing either:
  1. the number of elements on the LHS and RHS must be the same (e.g. three into three), or
  2. the RHS can be scalar.
Probably the easiest solution would be for you to use the second of these and treat each of the R, G, and B separately (as scalars).

1 Comment

Hi Stephen, My apologies, to clarify, what I intend to do with this code is to input a colour image and draw a line between two input coordinates (P1 and P2) and I want to assign RGB values to the line in order to give it a certain colour. Which is why I have them as:
cR=RGB(1);
cG=RGB(2);
cB=RGB(3);
because I want to be able to specify the values for Red, Green and Blue in order to get the desired colour when I use the function. Let's say the image we wanted to draw a line onto was a binary image and we wanted a white line, what we could have done was to have a line like:
imgColour(indices) = 255; %Assigning white to the drawn line.
Perhaps my logic is wrong when it comes to a colour image.

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 4 Sep 2015

Edited:

on 4 Sep 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!