読み込んだ画像の行と​列それぞれにグリッド​線を引いて、そのブロ​ック内ごとの平均を求​める方法に関して

13 views (last 30 days)
読み込んだ画像の行と列それぞれにグリッド線を引いて、そのブロック内ごとの平均を求める方法はありますか。

Accepted Answer

Atsushi Ueno
Atsushi Ueno on 3 Feb 2022
I = imread('peppers.png'); % 対象画像データ読込
sz = size(I); % 画像データのサイズ
bk = round(sz(1:2)./[8 8]); % 個別ブロックのサイズ: 画像を縦横8分割する
fun = @(blk) mean(blk.data,[1 2]); % 処理内容を標準偏差から平均に変更
I2 = blockproc(I,bk,fun) % ブロック処理演算を実行(縦横8分割したので8*8*RGBの結果になる)
I2 =
I2(:,:,1) = 66.1270 67.9613 71.0247 72.5202 71.6709 69.2842 67.3704 62.3952 66.2790 66.6396 69.2269 96.7793 79.0114 91.7155 73.8311 65.1263 66.8271 98.1543 109.0423 152.5482 124.6113 106.1859 125.3997 125.0267 73.0986 109.9033 146.2015 195.6803 180.3519 177.9808 224.5706 190.7090 175.3278 195.3454 171.0234 231.3447 225.3109 172.2396 182.1943 135.0312 189.0482 173.5840 207.8652 147.8665 98.7428 159.4824 188.7982 195.2288 171.5072 110.7796 105.8428 78.2367 80.9893 91.7386 109.7633 126.4271 99.9749 90.6084 110.1302 70.6403 72.8994 75.5749 65.2305 57.9401 I2(:,:,2) = 34.5954 36.8994 39.7288 40.9395 40.3877 38.0872 35.6602 31.4704 35.5586 37.4469 40.5654 67.2790 52.4281 67.5859 45.8519 35.4434 35.5938 93.2839 99.4743 120.4170 119.4655 99.4271 69.4671 59.5234 36.9860 72.2510 40.5078 115.3874 145.3369 93.9062 71.2738 52.2686 92.9893 123.1527 77.3314 141.2650 121.9193 52.6758 24.0954 17.1432 91.7773 76.8893 153.5755 80.8551 82.5355 35.6071 82.5658 157.5439 38.9639 44.1774 68.3086 55.1087 55.9652 49.0967 73.3359 96.0173 46.6458 52.4368 69.7461 39.9691 39.4128 40.7708 33.6426 29.3955 I2(:,:,3) = 66.0853 68.2165 70.9167 72.1276 70.8294 68.0853 65.9297 61.3665 66.8822 67.2552 69.4277 86.7702 65.9268 58.7562 60.9154 60.8698 65.6572 36.5029 37.7913 81.6051 23.5846 19.1774 27.0876 33.0915 59.3763 24.1315 39.4056 39.5355 25.4714 58.8861 36.3538 18.3932 24.8496 36.1201 55.2015 15.6683 14.4010 39.0365 34.6113 32.8132 34.6234 35.7399 76.1514 48.1751 35.6995 31.7096 82.4486 158.3958 35.1908 49.4440 56.2526 75.2493 82.0736 84.2467 92.8874 103.1289 86.0020 95.8044 123.7057 76.0638 71.3490 73.0443 64.3128 57.7337
  • image processing toolboxが利用不可の場合:MATLABのみで実行可能な例を示します。
for x = 1:bk(2):sz(2)
for y = 1:bk(1):sz(1)
I3((y-1)/bk(1)+1,(x-1)/bk(2)+1,:) ... % 平均値を計算(8*8*RGBの結果になる)
= mean(I(y:min(y+bk(1)-1,sz(1)),x:min(x+bk(2)-1,sz(2)),:),[1 2]);
end
end
I3
I3 =
I3(:,:,1) = 66.1270 67.9613 71.0247 72.5202 71.6709 69.2842 67.3704 62.3952 66.2790 66.6396 69.2269 96.7793 79.0114 91.7155 73.8311 65.1263 66.8271 98.1543 109.0423 152.5482 124.6113 106.1859 125.3997 125.0267 73.0986 109.9033 146.2015 195.6803 180.3519 177.9808 224.5706 190.7090 175.3278 195.3454 171.0234 231.3447 225.3109 172.2396 182.1943 135.0312 189.0482 173.5840 207.8652 147.8665 98.7428 159.4824 188.7982 195.2288 171.5072 110.7796 105.8428 78.2367 80.9893 91.7386 109.7633 126.4271 99.9749 90.6084 110.1302 70.6403 72.8994 75.5749 65.2305 57.9401 I3(:,:,2) = 34.5954 36.8994 39.7288 40.9395 40.3877 38.0872 35.6602 31.4704 35.5586 37.4469 40.5654 67.2790 52.4281 67.5859 45.8519 35.4434 35.5938 93.2839 99.4743 120.4170 119.4655 99.4271 69.4671 59.5234 36.9860 72.2510 40.5078 115.3874 145.3369 93.9062 71.2738 52.2686 92.9893 123.1527 77.3314 141.2650 121.9193 52.6758 24.0954 17.1432 91.7773 76.8893 153.5755 80.8551 82.5355 35.6071 82.5658 157.5439 38.9639 44.1774 68.3086 55.1087 55.9652 49.0967 73.3359 96.0173 46.6458 52.4368 69.7461 39.9691 39.4128 40.7708 33.6426 29.3955 I3(:,:,3) = 66.0853 68.2165 70.9167 72.1276 70.8294 68.0853 65.9297 61.3665 66.8822 67.2552 69.4277 86.7702 65.9268 58.7562 60.9154 60.8698 65.6572 36.5029 37.7913 81.6051 23.5846 19.1774 27.0876 33.0915 59.3763 24.1315 39.4056 39.5355 25.4714 58.8861 36.3538 18.3932 24.8496 36.1201 55.2015 15.6683 14.4010 39.0365 34.6113 32.8132 34.6234 35.7399 76.1514 48.1751 35.6995 31.7096 82.4486 158.3958 35.1908 49.4440 56.2526 75.2493 82.0736 84.2467 92.8874 103.1289 86.0020 95.8044 123.7057 76.0638 71.3490 73.0443 64.3128 57.7337
imshow(I); hold on; % 読み込んだ画像の行と列それぞれにグリッド線を引く
for x = 1:bk(2):sz(2)
line([x x],[0 sz(1)],'color','k');
end
for y = 1:bk(1):sz(1)
line([0 sz(2)],[y y],'color','k');
end
  6 Comments
宗樹 岡本
宗樹 岡本 on 5 Feb 2022
ご教授ありがとうございます。プログラムについて理解でき、やりたいこともできるようになりました。ありがとうございます。

Sign in to comment.

More Answers (1)

Hernia Baby
Hernia Baby on 3 Feb 2022
カラーは面倒なので、
サンプルをグレースケールで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)
A = 16×1
100.4229 102.3418 106.4229 93.3516 79.6152 71.2744 80.4141 98.5723 125.0586 153.4229
ここからがめんどくさいです
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))
  1 Comment
宗樹 岡本
宗樹 岡本 on 4 Feb 2022
ご教授ありがとうございます。勉強になりました

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!