Problem with Matlab sliding neighborhoods

1 回表示 (過去 30 日間)
Efstathios Kontolatis
Efstathios Kontolatis 2016 年 12 月 9 日
編集済み: DGM 2024 年 8 月 29 日
I have an image. I want to create a sliding window of changing size and want to make some calculations depending on some criteria. For example I initialize window size=3 and then calculate the min max and median in that window for a pixel. If the criteria are not ok I want to use window size=5 etc. This depends on the pixel's neighbors and I want to do that for every pixel. So I don't want to calculate something with nfilter since the window size is going to change for every pixel.

回答 (1 件)

Shubham
Shubham 2024 年 8 月 29 日
Hi Efstathios,
To implement a sliding window of varying size over an image in MATLAB, you can manually iterate over each pixel and adjust the window size based on specific criteria. Here’s a step-by-step approach to achieve this:
  1. Start with an initial window size and specify the criteria for adjusting the window size.
  2. Loop through each pixel in the image, apply the sliding window, and perform the necessary calculations.
  3. Based on the calculated values (e.g., min, max, median), adjust the window size and recalculate if needed.
Here's a sample MATLAB script to illustrate this process:
% Load an example image (grayscale for simplicity)
img = imread('example.jpg');
img = rgb2gray(img); % Convert to grayscale if it's a color image
[rows, cols] = size(img);
% Initialize parameters
initial_window_size = 3;
max_window_size = 11; % Define a maximum window size to prevent excessive growth
% Criteria thresholds (example values, adjust as needed)
min_threshold = 10;
max_threshold = 245;
median_threshold = 127;
% Create an output image to store results or modified values
output_img = zeros(size(img));
% Iterate over each pixel in the image
for r = 1:rows
for c = 1:cols
window_size = initial_window_size;
criteria_met = false;
while ~criteria_met && window_size <= max_window_size
% Calculate window boundaries
half_window = floor(window_size / 2);
r_min = max(1, r - half_window);
r_max = min(rows, r + half_window);
c_min = max(1, c - half_window);
c_max = min(cols, c + half_window);
% Extract the window
window = img(r_min:r_max, c_min:c_max);
% Calculate min, max, and median
window_min = min(window(:));
window_max = max(window(:));
window_median = median(window(:));
% Check criteria
if window_min > min_threshold && window_max < max_threshold && window_median > median_threshold
criteria_met = true;
else
window_size = window_size + 2; % Increase window size by 2 (ensure it remains odd)
end
end
% Store the result in the output image (e.g., median value)
output_img(r, c) = window_median;
end
end
% Display the result
imshow(uint8(output_img)); % Convert to uint8 for display
Explanation:
  • Initial Window Size: Start with a small window size and adjust as needed.
  • Window Boundaries: Ensure that the window does not exceed the image boundaries.
  • Criteria Check: Adjust window size based on whether the calculated values meet specified criteria.
  • Output Image: Store results in an output image for further analysis or visualization.
This script assumes you have a grayscale image. If you are working with a color image, you can modify the script to handle each color channel separately or convert the image to grayscale as shown. Adjust the criteria and thresholds according to your specific needs.
  1 件のコメント
DGM
DGM 2024 年 8 月 29 日
編集済み: DGM 2024 年 8 月 29 日
What is the goal of this filter? It's roughly similar to an adaptive median noise removal filter, but the window scaling is never reset, and there is no noise discrimination test to tell us whether the current pixel is noise. In fact, the pixel under test is never used.
In the end, it's more or less a complicated median spatial filter with an irregularly (but monotonically) increasing kernel size.
I guess that's fair. OP isn't here to tell us what they actually wanted, but I don't know where this would be a useful filter.

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

Community Treasure Hunt

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

Start Hunting!

Translated by