Create a mosaic using average color of block given image

5 ビュー (過去 30 日間)
Shadi Khansa
Shadi Khansa 2020 年 10 月 19 日
コメント済み: Shadi Khansa 2020 年 11 月 16 日
I am required to read an image and generate an 8x8 mosaic using the average color of each block. This is my work so far, however the output is black and white. I need help to get the colors. Thank you.
smashpic = imread('PA6_smashing.png');
myfun = @(block_struct) mean2(block_struct.data) *...
ones(size(block_struct.data),class(block_struct.data));
mosaic = blockproc(smashpic,[8 8],myfun);
imshow(mosaic);

採用された回答

Swatantra Mahato
Swatantra Mahato 2020 年 10 月 22 日
Hi,
I am assuming you want to get the average of color of each block for each component separately. In your function "myfun" by using "mean2()" you get the mean of the entire block as a single value which is then used for R,G and B values.
One way to get the mosaic in color would be to calculate the average for each color of the block separately and then combine them using the "cat" function. Here is an example :
smashpic = imread('peppers.png');
myfun = @(block_struct) cat(3,mean2(block_struct.data(:,:,1))*ones(block_struct.blockSize,class(block_struct.data))...
,mean2(block_struct.data(:,:,2))*ones(block_struct.blockSize,class(block_struct.data))...
,mean2(block_struct.data(:,:,3))*ones(block_struct.blockSize,class(block_struct.data)));
%myfun = @(block_struct) mean2(block_struct.data) *...
%ones(size(block_struct.data),class(block_struct.data)); %resulting image is black and white
mosaic = blockproc(smashpic,[8 8],myfun);
imshow(mosaic);
Hope this helps

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by