Image patch extraction from given co-ordinate

Hello everyone, I have an image [800 x 900 x 4]. I have to extract patches with a center point [(x1,y1),(x2,y2)... ]. How can I do that ?.
Thanks

2 件のコメント

Rik
Rik 2018 年 1 月 25 日
What size should those patches be? And is your image a 3D gray scale image, or some other format? (YMCK perhaps?)
MD RESHAD UL HOQUE
MD RESHAD UL HOQUE 2018 年 1 月 25 日
Patches size 17 x 17. It is 4D image.

サインインしてコメントする。

 採用された回答

Rik
Rik 2018 年 1 月 25 日
編集済み: Rik 2018 年 1 月 29 日

1 投票

Your image is not 4D. It has 3 dimensions, unless what you posted in your question is not true.
patch{i}=IM(x(i)+[-8 8],y(i)+[-8 8],:);
This makes use of the quirk of Matlab that if you enter corner coordinates, it will return the entire square part.
For edge cases, the code below crops the part outside of the matrix (my comment below has a copy-and-past bug)
selected_patches = cell(length(x),1);
for i=1:length(x)
xtemp=x(i)+[-8 8];
xtemp(xtemp<1)=1;xtemp(xtemp>size(IM,1))=size(IM,1);
ytemp=y(i)+[-8 8];
ytemp(ytemp<1)=1;ytemp(ytemp>size(IM,2))=size(IM,2);
selected_patches{i}=IM(xtemp,ytemp,:);
end

8 件のコメント

MD RESHAD UL HOQUE
MD RESHAD UL HOQUE 2018 年 1 月 25 日
I am sorry. The image has 4 channel, not 4D. So I followed your instruction. But I get an error. Index exceeds matrix dimensions. Here is my code : [pnts means given co-ordinates]
x=zeros(length(pnts),1);
y=zeros(length(pnts),1);
for i=1:length(pnts)
x(i)=pnts(i);
y(i)=pnts(i,2);
end
%patch=zeros(length(x),2);
patch=[];
for i=1:length(x)
%patch(i) = IM(x(i)-16:x(i)+16,y(i)-16:y(i)+16,:);
patch{i}=IM(x(i)+[-8 8],y(i)+[-8 8],:);
%patch(i)=IM(x(i)+[-8 8],y(i)+[-8 8],:);
end
Walter Roberson
Walter Roberson 2018 年 1 月 25 日
What do you want to do in the case that the center of a requested patch is less than 8 from a border?
MD RESHAD UL HOQUE
MD RESHAD UL HOQUE 2018 年 1 月 25 日
編集済み: MD RESHAD UL HOQUE 2018 年 1 月 25 日
That can happen. But I check the coordinate (x,y) and the center is not less than 8.
Walter Roberson
Walter Roberson 2018 年 1 月 25 日
You have to worry about the bottom and right edges too.
If the supplied coordinates in x and y are too close to the border to give a full 17 x 17 patch centered on x(i), y(i), then what do you want the program to do ? Do you want it to produce a partial patch, whatever fits? Do you want it to produce an error message? Do you want it to still take 17 x 17 by adjusting the coordinates to fit?
MD RESHAD UL HOQUE
MD RESHAD UL HOQUE 2018 年 1 月 25 日
In that case, maybe partial patch. or maybe adjusting by zero padding I guess.
Rik
Rik 2018 年 1 月 25 日
For a partial patch you can use the code below.
xtemp=x(i)+[-8 8];
xtemp(xtemp<1)=1;xtemp(xtemp>size(IM,1))=size(IM,1);
ytemp=x(i)+[-8 8];
ytemp(ytemp<1)=1;ytemp(ytemp>size(IM,1))=size(IM,1);
patch{i}=IM(xtemp,ytemp,:);
MD RESHAD UL HOQUE
MD RESHAD UL HOQUE 2018 年 1 月 25 日
編集済み: Walter Roberson 2018 年 1 月 25 日
In that case, patch initialization will be like this or different :
patch=zeros(length(x),2);
or
Patch=[];
Pardon me I am new in matlab.
Thanks
Walter Roberson
Walter Roberson 2018 年 1 月 25 日
patch = cell(length(x),1);

サインインしてコメントする。

その他の回答 (0 件)

質問済み:

2018 年 1 月 25 日

編集済み:

Rik
2018 年 1 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by