why X and Y coordinates must have the same size ?

hello,
I have an image and a shape and I need to clip the image based on the coordinates of the shape but when I do this I am having an error says: x and y must be the same size how can I make them equal? or any idea of solving this issue please ?
Thanks

5 Commenti

Why don't you provide your code.....so that we can have more clear idea..
the image is very large I can't attach it here I have attached the error that I'm having and here is the code :
info = geotiffinfo('SVDNB_npp_20121201-20121231_75N060W_vcmcfg_v10_c201601041440.avg_rade9h.tif');
%disp(info);
[x1,y1]= pixcenters(info);
%pix2map(info.RefMatrix,1,1)
shape = shaperead(fullfile('KTAF_sat_shapefiles','shapefiles','SAU0.csv.shp'));
x2=shape.X;
y2=shape.Y;
%numel(x)
%numel(y)
[x3,y3]= polyclip(x1,y1,x2,y2,1);
function [X,Y] = polyclip(x1,y1,x2,y2,method) % POLYCLIP Clip two polygons (intersect, union, xor, diff).
%
% [X,Y] = POLYCLIP(X1,Y1,X2,Y2,METHOD) clips the two polygons given by the % points (X1(i),Y1(i)) and (X2(j),Y2(j)) according to the METHOD:
% METHOD = 0 or 'dif' ==> difference (P1-P2)
% METHOD = 1 or 'int' ==> intersection
% METHOD = 2 or 'xor' ==> Xor
% METHOD = 3 or 'uni' ==> Union
% X and Y are cell arrays (because the result may be multiple polygons) % containing the x and y coordinates of the resulting polygon(s).
%
% XY = POLYCLIP([X1 Y1],[X2 Y2],METHOD) does the same assuming the Xi and Yi % are column vectors. The result is {X Y}.
%
% See also CLIPPER, POLYOUT.
% Copyright (c)2015-17, Prof. Erik A. Johnson <JohnsonE@usc.edu>, 01/29/17
% 09/13/15 EAJ Initial code
% 01/28/17 EAJ Update for newer MATLAB versions % 01/29/17 EAJ Fix: test for METHOD as string, single output
narginchk(2,5);
if any(nargin==[2 4]), method=[]; end
if any(nargin==[2 3]) % POLYCLIP([X1 Y1],[X2 Y2],METHOD)
if size(x1,2)~=2 || size(y1,2)~=2 || ~isnumeric(x1) || ~isnumeric(y1),
error('The [X1 Y1] and [X2 Y2] matrices must be numeric two-column matrices.');
end
if nargin == 3
method = x2;
else
method = [];
end
y2=y1(:,2); x2=y1(:,1);
y1=x1(:,2); x1=x1(:,1);
else
if ~isnumeric(x1) || ~isnumeric(y1) || ~isnumeric(x2) || ~isnumeric(y2)
error('The polygon coordinates must be numeric matrices.')
elseif numel(x1)~=length(x1) || numel(y1)~=length(y1) || numel(x2)~=length(x2) || numel(y2)~=length(y2)
error('Function only handles the clipping of one polygon with another, not multiple polygons with multiple polygons');
elseif numel(x1)~=numel(y1) || numel(x2)~=numel(y2)
error('X1 must be the same size as Y1, and X2 the same size as Y2.');
end;
if length(x1)==numel(x1), x1=x1(:); y1=y1(:); end; % ensure column
if length(x2)==numel(x2), x2=x2(:); y2=y2(:); end; % ensure column
if nargin == 4
method = [];
end
end;
if isempty(method), method=0; end
if isstr(method)
find(lower(method(1))==['dixu']) - 1;
if isempty(ans)
error('METHOD must be an integer (0 to 3) or {''diff'',''int'',''xor'',''union''}.');
end;
method = ans;
end
if ~isscalar(method) || ~any(method==0:3)
error('METHOD must be 0, 1, 2 or 3.')
end;
scale = 2^32;
% leave this in array format just in case we later adapt this
pack = @(p) arrayfun(@(x) struct('x', int64(x.x*scale),'y', int64(x.y*scale)),p);
unpack = @(p) arrayfun(@(x) struct('x',double(x.x)/scale,'y',double(x.y)/scale),p);
poly1 = struct('x',num2cell(x1,1),'y',num2cell(y1,1));
poly2 = struct('x',num2cell(x2,1),'y',num2cell(y2,1));
assert(numel(poly1)==1, 'Only single polygon clippings are allowed in the current code');
poly3 = unpack(clipper(pack(poly1),pack(poly2),method));
if isempty(poly3)
x = cell(1,0);
y = cell(1,0);
else
x = {poly3.x};
y = {poly3.y};
end
% % package into single polygon with NaN's separating individual polygons % lens = cellfun(@numel,{poly3.x}'); % cellfun(@(x,l) [x;NaN(max(lens)-l,1)], {poly3.x}', num2cell(lens), 'UniformOutput',false); x=cat(2,ans{:}); % cellfun(@(y,l) [y;NaN(max(lens)-l,1)], {poly3.y}', num2cell(lens), 'UniformOutput',false); y=cat(2,ans{:});
if nargout>=2
X = x;
Y = y;
else
% X = cellfun(@(x,y) [x(:) y(:)],x,y,'UniformOutput',false);
X = {x y};
end
end
Is the actual error that you are getting
X1 must be the same size as Y1, and X2 the same size as Y2.
or are you getting a different error ?
Reema Alhassan
Reema Alhassan il 26 Giu 2018
Modificato: Reema Alhassan il 26 Giu 2018
yes,this is the error
What shows up for
size(x1)
size(y1)
size(x2)
size(y2)

Accedi per commentare.

 Risposta accettata

I suggest using poly2mask() to create mask (binary image) that you can then multiply by the original image to get the clipped image. You might always want find the bounding box of the mask and use that to crop the result.
Your x and y coordinates will need to be the same length for poly2mask.
That might involve deliberately repeating some coordinates. For example to express y ranging from 10 to 20 at x = 7, coming from (0,0), then you cannot just code x = [0 7], y = [0 10 20] -- each y must be matched with an x. You would instead code x = [0 7 7], y = [0 10 20]

11 Commenti

yes I got it, but the size of the coordinates very large I can't do it manually also I have to deal with more than one image .. is this something related to projection ? because I'm using geotiff image (satellite image ) and when I check the sizes of the x and y coordinate I always get different sizes ..
hey
here when you said "then multiply by the original image to get the clipped image" do you mean multiply it with the coordinates?
thanks
Try
mask = poly2mask(x, y, rows, columns);
% Mask the image using bsxfun() function to multiply the mask by each channel individually.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
Reema Alhassan
Reema Alhassan il 26 Giu 2018
Modificato: Reema Alhassan il 26 Giu 2018
what do you mean here by rows and columns ? could you give me an example please?
You said "I have an image" and so rows and columns would be the number of rows and columns in your image.
actually I have tried this code and it is working for the small images :
R = geotiffinfo('clipped2.tif');
f= geoimread('clipped2.tif');
% Get x,y locations of pixels:
[x,y] = pixcenters(R);
% Convert x,y arrays to grid:
[X,Y] = meshgrid(x,y);
roi = shaperead(fullfile('KTAF_sat_shapefiles','shapefiles','SAU0.csv.shp'));
% Remove trailing nan from shapefile
rx = roi.X(1:end-1);
ry = roi.Y(1:end-1);
mask =inpolygon(X,Y,rx,ry);
t= mask.*f;
sumOfValues= sum(t(:));
sumOfValues1=sprintf('%f', sumOfValues)
but I have to deal with larger images about 4GB so when I run it with large image the computer is crashing in either meshgrid() or in inpolygon()
Is it possible that each of your shapes has several embedded NaN? It is not uncommon to pack several shapes into one vector, especially if there are discontinuous pieces (such as if lakes or islands are outlined as well.)
I can take the coordinates of each polygon and deal with each one alone so this is not a problem
my problem is that the geotiff image is very large so I need to clip it first based on the boundingBox of the shape to make it smaller and then apply the function inpolygon() but I couldn't find a way to clip it and make a new geotiff image if you know how to do this could you help me please?
Thanks
"Expecting input number 1, X, to be finite"
is an error you are getting because you are feeding in a polygon that has NaN inside it, to mark the end of a segment such as to move over to draw a lake or island. You need to break up your inputs to poly2mask, creating several masks and or'ing them together.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by