読み込んだ画像の行と列それぞれにグリッド線を引いて、そのブロック内ごとの平均を求める方法に関して
Show older comments
読み込んだ画像の行と列それぞれにグリッド線を引いて、そのブロック内ごとの平均を求める方法はありますか。
Accepted Answer
More Answers (1)
カラーは面倒なので、
サンプルをグレースケールで128×128の画像にします
clc,clear,close all;
L = 128;
Img0 = imread('chacha_sq.jpeg');
Img0 = imresize(Img0,[L,L]);
Img = im2gray(Img0);
imshow(Img);
ブロックごとの平均を求めるのは簡単です
n行,m列ずつ区切りましょう
n = 32;
m = 32;
I = reshape(Img,n,m,[]);
A = reshape(mean(I,[1 2]),[],1,1)
ここからがめんどくさいです
n行ごとに1行分の黒線を追加し、m列ごとに1列分の黒線を追加します。
in = L/n;
jm = L/m;
IMG1 = [];
for ii = 1:in
tmp = cat(1,Img(1+n*(ii-1):n*ii,:),zeros(1,L));
IMG1 = [IMG1;tmp];
end
IMG2 = [];
clear tmp
for jj = 1:jm
tmp = cat(2,IMG1(:,1+m*(jj-1):m*jj),zeros(L+in,1));
IMG2 = [IMG2,tmp];
end
imshow(IMG2)
本当はreshapeで分割したものを戻したかったのですが、復元がうまくかず…
[i1,j1,k1] = size(I);
I1 = cat(1,I,zeros(1,j1,k1));
I1 = cat(2,I1,zeros(i1+1,1,k1));
[i2,j2,k2]=size(I1);
ll = sqrt(i2*j2*k2);
imshow(reshape(I1,ll,ll,1))
Categories
Find more on Computer Vision Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



