フィルターのクリア

Block Processing without Builtin blockproc

2 ビュー (過去 30 日間)
Tiffany
Tiffany 2022 年 10 月 6 日
回答済み: Image Analyst 2023 年 10 月 27 日
Hi,
I'm trying to build the blocproc as an algorithm to understand how it works.
I'm sliding a 3x3 window over the image, and applying the kernel to that block, then, moving to the next block.
I have this so far, it's not writing anything to the array with the for loop. Can someone take a look and see what I'm missing?
Thanks in advance.
img1 = 'cman.tif';
%read image in workspace
img1 = imread(img1);
imshow(img1)
kernel = .0625 * [1 2 1; 2 4 2; 1 2 1];
%%%%%%%%%%%%%%%%%%%
m = 3;
n = 3; %this is the 3x3 block that will iterate over image
img_temp = [];
[num_rows num_columns depth] = size(img1);
col_win_counter = 0;
for col = 1:n:num_cols
col_win_counter = col_win_counter +1;
row_win_counter = 0;
col_start = col;
col_stop = min(col+n-1,num_cols);
for row = 1:m:num_rows
row_win_counter = row_win_counter +1;
window = zeros(m,n);
row_start = row;
row_stop = min(row+m-1, num_rows);
window(1:row_stop-row_start+1,1:col_stop-col_start + 1) = ...
img1(row_start:row_stop, col_start:col_stop);
img_temp = mtimes(window, kernel);
end
end
spatial_img = num2cell(img_temp);
imshow(spatial_img);
  6 件のコメント
Tiffany
Tiffany 2022 年 10 月 7 日
I also corrected the naming error of num_columns to num_cols in line 18. Sorry about that.
This is the error, after closing out Matlab and re-running it:
Error using *
MTIMES (*) is not fully supported for integer classes. At least one argument must be
scalar.
Thank you again.
J. Alex Lee
J. Alex Lee 2022 年 10 月 7 日
You are trying to fit a matrix of size 3x3 into an array of size 1x1, and it's also unclear whether you actually want that matrix times (*) vs element-wise times (.*).
If you actually meant element-wise, and you want to save the result into the original 3x3 slot of your image (and treat the edges specially when you don't have the full neighborhood), that's a convolution.
If you wanted to somehow collapse your 3x3 neighborhood into some single scalar and insert it into img_tmp at your counter location, that looks more like a block proc. However, what is the operation you actually want to do to collapse your 3x3 neighborhood into a scalar.

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

回答 (1 件)

Image Analyst
Image Analyst 2023 年 10 月 27 日
Looks like the other (chatbot) answer didn't get it fully correct.
You need to do the multiplication the correct way and then sum the result. Plus, you reversed the order of the row and column indexes in img_temp for some reason -- row goes first, not column. Additionally you should preallocate your output image otherwise it does it for every single pixel as you assign a new location and it will take a lot longer.
Corrected code is below:
% Demo by Image Analyst manually block process a gray scale image to downsample it.
% Modified version of what the user posted on Answers (not how I'd do it).
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 22;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
originalImage = 'cameraman.tif';
% Read image from disk.
originalImage = imread(originalImage);
subplot(1, 2, 1);
imshow(originalImage)
axis('on', 'image');
title('Original image')
impixelinfo; % Let user mouse around and see location and gray level.
kernel = .0625 * [1 2 1; 2 4 2; 1 2 1]; % Will blur the image
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
myWindowHeight = 3;
nxWindowWidth = 3; %this is the 3x3 block that will iterate over image
[num_rows, num_columns, depth] = size(originalImage);
outputRows = floor(num_rows / myWindowHeight)
outputRows = 85
outputColumns = floor(num_columns / nxWindowWidth)
outputColumns = 85
% Preallocate the output image.
outputImage = zeros(outputRows, outputColumns);
col_win_counter = 0;
for col = 1 : nxWindowWidth : num_columns
fprintf('Processing Row #%d of %d.\n', col, num_columns)
col_win_counter = col_win_counter +1;
row_win_counter = 0;
% Get starting and stopping columns of the current pixel location.
col_start = col;
col_stop = min(col+nxWindowWidth-1, num_columns); %stops before edge
% Go down all rows in this column summing the values in the local window.
for row = 1 : myWindowHeight : num_rows
row_win_counter = row_win_counter +1;
% Get starting and stopping rows of the current pixel location.
row_start = row;
row_stop = min(row+myWindowHeight-1, num_rows);
% Extract the local 3x3 window below and to the right of the current pixel.
thisLocalWindow = double(originalImage(row_start:row_stop, col_start:col_stop));
outputImage(row_win_counter, col_win_counter) = sum(thisLocalWindow .* kernel, 'all');
end
end
Processing Row #1 of 256. Processing Row #4 of 256. Processing Row #7 of 256. Processing Row #10 of 256. Processing Row #13 of 256. Processing Row #16 of 256. Processing Row #19 of 256. Processing Row #22 of 256. Processing Row #25 of 256. Processing Row #28 of 256. Processing Row #31 of 256. Processing Row #34 of 256. Processing Row #37 of 256. Processing Row #40 of 256. Processing Row #43 of 256. Processing Row #46 of 256. Processing Row #49 of 256. Processing Row #52 of 256. Processing Row #55 of 256. Processing Row #58 of 256. Processing Row #61 of 256. Processing Row #64 of 256. Processing Row #67 of 256. Processing Row #70 of 256. Processing Row #73 of 256. Processing Row #76 of 256. Processing Row #79 of 256. Processing Row #82 of 256. Processing Row #85 of 256. Processing Row #88 of 256. Processing Row #91 of 256. Processing Row #94 of 256. Processing Row #97 of 256. Processing Row #100 of 256. Processing Row #103 of 256. Processing Row #106 of 256. Processing Row #109 of 256. Processing Row #112 of 256. Processing Row #115 of 256. Processing Row #118 of 256. Processing Row #121 of 256. Processing Row #124 of 256. Processing Row #127 of 256. Processing Row #130 of 256. Processing Row #133 of 256. Processing Row #136 of 256. Processing Row #139 of 256. Processing Row #142 of 256. Processing Row #145 of 256. Processing Row #148 of 256. Processing Row #151 of 256. Processing Row #154 of 256. Processing Row #157 of 256. Processing Row #160 of 256. Processing Row #163 of 256. Processing Row #166 of 256. Processing Row #169 of 256. Processing Row #172 of 256. Processing Row #175 of 256. Processing Row #178 of 256. Processing Row #181 of 256. Processing Row #184 of 256. Processing Row #187 of 256. Processing Row #190 of 256. Processing Row #193 of 256. Processing Row #196 of 256. Processing Row #199 of 256. Processing Row #202 of 256. Processing Row #205 of 256. Processing Row #208 of 256. Processing Row #211 of 256. Processing Row #214 of 256. Processing Row #217 of 256. Processing Row #220 of 256. Processing Row #223 of 256. Processing Row #226 of 256. Processing Row #229 of 256. Processing Row #232 of 256. Processing Row #235 of 256. Processing Row #238 of 256. Processing Row #241 of 256. Processing Row #244 of 256. Processing Row #247 of 256. Processing Row #250 of 256. Processing Row #253 of 256. Processing Row #256 of 256.
% Display floating point result image.
subplot(1, 2, 2);
imshow(outputImage, [])
axis('on', 'image');
impixelinfo; % Let user mouse around and see location and gray level.
title('Block processed image')
fprintf('Done with script.\n')
Done with script.

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by