Info

This question is closed. Reopen it to edit or answer.

what is the error here?

1 view (last 30 days)
esraa
esraa on 28 Nov 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
i want to make a mode filter for the red channel of an image by using a simple GUI i wrote my code and it doesn't work
function mod_filter_Callback(hObject, eventdata, handles)
global image;
red=image(:,:,1);
modeImage=colfilt(red, [5 5], 'sliding', @mode);
axes(handles.axes16);
imshow(modeImage,[]);
  1 Comment
Adam
Adam on 28 Nov 2014
It may not be causing a problem here, but don't use builtin function names ( 'image' ) as variable names.
Given it is a global variable I have no idea what kind of mess that could cause, maybe no more than a normal variable, but I would strongly advise against using global variables in any case.
You failed to say in what way your code "doesn't work" though. It is a lot easier for us to help if you give us full information!

Answers (1)

Image Analyst
Image Analyst on 28 Nov 2014
Edited: Image Analyst on 28 Nov 2014
You can use blockproc():
% Demo code to divide the image up into 5 pixel by 5 pixel blocks
% and replace each pixel in the block by the mode
% of all the gray levels of the pixels in the block.
%
clc;
clearvars;
close all;
workspace;
fontSize = 16;
% Read in a standard MATLAB gray scale demo image.
folder = fileparts(which('cameraman.tif'));
if ~exist(folder, 'dir')
% If that folder does not exist, don't use a folder
% and hope it can find the image on the search path.
folder = [];
end
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage)
% Display the original gray scale image.
subplot(1, 2, 1);
imshow(grayImage, []);
caption = sprintf('Original Grayscale Image\n%d rows by %d columns',...
rows, columns);
title(caption, 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Image Analysis Demo','numbertitle','off')
% Block process the image to replace every pixel in the
% 5 pixel by 5 pixel block by the mode
% of the pixels in the block.
% Image will be smaller since we are not using ones() and so for each block
% there will be just one output pixel, not a block of 5 by 5 output pixels.
blockSize = [5 5];
modeFilterFunction = @(theBlockStructure) mode(double(theBlockStructure.data(:)));
blockyImageMode = blockproc(grayImage, blockSize, modeFilterFunction);
[rows, columns] = size(blockyImageMode);
% Display the block mode filtered image.
subplot(1, 2, 2);
imshow(blockyImageMode, []);
title('Mode Filtered Image', 'FontSize', fontSize);
caption = sprintf('Block Mode Filtered Image\n32 blocks. Input block size = %d, output block size = 1\n%d rows by %d columns',...
blockSize(1), rows, columns);
title(caption, 'FontSize', fontSize);
  2 Comments
esraa
esraa on 28 Nov 2014
Edited: Image Analyst on 28 Nov 2014
i tried this but still not working
global img;
red=img(:,:,1);
blockSize = [5 5];
StDevFilterFunction = @(theBlockStructure) mode(double(theBlockStructure.data(:)));
blockyImageMode = blockproc(red, blockSize, StDevFilterFunction);
imshow(blockyImageMode, []);
axes(handles.axes16);
Image Analyst
Image Analyst on 28 Nov 2014
What does it do? Can you attach your image?

Community Treasure Hunt

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

Start Hunting!