How it works?
6 views (last 30 days)
Show older comments
Hi, can anyone help to explain this code? It's a Oil Painting code taken online...Thanks
A=imread('aish.jpg');
%Define the matrix size of your convience.
m=5;
n=6;
Image=uint8(zeros([size(A,1)-m,size(A,2)-n,3]));
%Calculate the histogram for each RGB value.
for v=1:3
for i=1:size(A,1)-m
for j=1:size(A,2)-n
mymask=A(i:i+m-1,j:j+n-1,v);
h=zeros(1,256);
for x=1:(m*n)
h(mymask(x)+1)=h(mymask(x)+1)+1;
end
%Maximum occurring value and the position is obtained
[maxvalue,pos]=max(h);
Image(i,j,v)=pos-1;
end
end
end
figure,imshow(Image);
4 Comments
Image Analyst
on 4 Apr 2012
Superb, note the comma so it's a:b, c:d and not a:b:c. See my explanation below.
Answers (1)
Image Analyst
on 4 Apr 2012
It looks like they go along the image a pixel at a time, calculating the histogram of a subimage (located with its upper left pixel at the scanning pixel location) separately, then logging the value of the most frequently occurring graylevel for each color channel in a new image at the location of the scaning pixel.
mymask=A(i:i+m-1,j:j+n-1,v);
takes a rectangular block out of the A image going from row1 = i to row2 = (i+m-1). Same for the columns, so it basically gets a little rectangular chunk out of the larger, original image.
The code is not well commented and has poorly chosen variable names. I cleaned it up a bit. Try this version:
% Script to turn an RGB image into a spatially quantized version of it
% that has the appearance of a painting.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
title('Original Color Image', 'FontSize', fontSize);
drawnow;
%Define the window size.
windowSizeVert = 15;
windowSizeHoriz = 11;
% Initialize an output image.
paintedImage=uint8(zeros([rows-windowSizeVert, columns-windowSizeHoriz, 3]));
%Calculate the histogram for each RGB value.
for colorChannel = 1 : numberOfColorBands
for row = 1: rows - windowSizeVert
for column = 1 : columns - windowSizeHoriz
% Get a little sub-image out.
% The sub-image has it's upper left corner at pixel location i,j.
subImage = rgbImage(row:row+windowSizeVert-1, column:column+windowSizeHoriz-1, colorChannel);
% If you want to see the sub image, uncomment this
% but it slows it down A LOT!
% subplot(2, 2, 2);
% imshow(subImage);
% drawnow;
% Compute the histogram by incrementing values.
histOfWindow = zeros(1,256); % Initialize
for x = 1:(windowSizeVert * windowSizeHoriz)
histOfWindow(subImage(x)+1) = histOfWindow(subImage(x)+1) + 1;
end
% Maximum occurring value and the position is obtained
% for this color channel.
[maxvalue, indexAtMax] = max(histOfWindow);
% Assign the intensity value, which is the index at the max - 1
% since the indexes go from 1-356 but the intensities gto from 0-255.
paintedImage(row,column,colorChannel) = indexAtMax - 1;
end
fprintf('Processed row %d of %d in color channel #%d of %d\n', ...
row, rows, colorChannel, numberOfColorBands);
end
end
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
% Now we have our output image, so display it.
subplot(2, 2, 3);
imshow(paintedImage);
axis on;
title('"Painted" Image', 'FontSize', fontSize);
msgbox('Done with demo');
0 Comments
See Also
Categories
Find more on Convert Image Type 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!