split image and create a new one using this pieces

how can I split an image into N parts and create a new one mixing this pieces. Example:
Take and CT image of 128x128, split it in 16 parts of 32x32 and create a new artificial CT image mixing this pieces.

回答 (1 件)

Jon
Jon 2022 年 5 月 2 日

1 投票

Does this do what you want:
% make an example image
A = randi(128,128);
% chop it up into 32 x 32 squares
B = reshape(A,32,32,16);
% rearrange the squares
C = B(:,:,randperm(16));
% put them back together as an image
C = reshape(C,128,128);

11 件のコメント

Jon
Jon 2022 年 5 月 2 日
You don't need to have the intermediary matrices B and C if you don't need to save the original A, so instead you could have
% make an example image
A = randi(128,128);
% chop it up into 32 x 32 squares
A = reshape(A,32,32,16);
% rearrange the squares
A = A(:,:,randperm(16));
% put them back together as an image
A = reshape(A,128,128);
Sergio José Uc Magaña
Sergio José Uc Magaña 2022 年 5 月 2 日
sorry, im new usint Matlab. Should I declare "A" as my image first? Example, my image in "CT000100.dcm",
A = dicomread("CT000100.dcm")
% make an example image
A = randi(128,128);
imshow(A)
% chop it up into 32 x 32 squares
B = reshape(A,32,32,16)
% rearrange the squares
C = B(:,:,randperm(16))
% put them back together as an image
C = reshape(C,128,128)
imsave
Jon
Jon 2022 年 5 月 2 日
Yes you should assign A using your image, I just used randi as an example.
In your example above you assign A to your image, as you should, but then you should delete the next line,
A = randi(128,128);
This is left over from my example, and it will overwrite whatever image you put into A
Sergio José Uc Magaña
Sergio José Uc Magaña 2022 年 5 月 2 日
Ok, then when i run the code, display "number of elements must not change. Use [] as one of the size inputs to automatically calculate the apropriate size for that dimension". I understand that in "reshape(A,32,32,16)", you apply "reshape" to "A" and split the image in 16 squeres of 32x32
Jon
Jon 2022 年 5 月 3 日
The reshaping can not change the total number of elements. The error means that your A matrix does not have 128*128 = 32*32*16 =16384 elements. So please check what size your A matrix is. For example use
[m,n] = size(A)
Sergio José Uc Magaña
Sergio José Uc Magaña 2022 年 5 月 3 日
I fixed that, the code runs great, but the final image is a 1x16 array, it is made up of 16 horizontal strips, I mean, should be an image of 16 32x32 squares
Walter Roberson
Walter Roberson 2022 年 5 月 3 日
jpg images are nearly always rgb images even when they look grey. True grey jpg are possible, but because the first release of the JPEG standard did not allow them, most programs write them as rgb for compatability.
Sergio José Uc Magaña
Sergio José Uc Magaña 2022 年 5 月 3 日
due to the jpeg format I can't make the final image 4x4 squares of 32x32?
Walter Roberson
Walter Roberson 2022 年 5 月 3 日
check size() of the image with only one output to size() and see whether you get back a vector of length 2 or 3.
Walter Roberson
Walter Roberson 2022 年 5 月 3 日
reshape(32,32,16) does not mean that 32 by 32 squares should be chopped out of the array. It means that the first 1024 (32*32) elements in memory should be put into the first plane, the next 1024 into the second plane, and so on. With you having resized to 128 x 128, the first 1024 entries in memory are the first 8 rows.
Use mat2cell to break up into tiles, or there is a File Exchange contribution... im2tiles or some name like that.
Jon
Jon 2022 年 5 月 3 日
Thanks @Walter Roberson. I'm really sorry, for sending you down the wrong path on this. I didn't really think this through. As Walter explains, the reshaping will take the values columnwise out of the original array, not what you wanted.

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

カテゴリ

ヘルプ センター および File ExchangeImage Processing and Computer Vision についてさらに検索

タグ

質問済み:

2022 年 5 月 2 日

コメント済み:

Jon
2022 年 5 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by