Image processing Image rotation

4 views (last 30 days)
Dipak
Dipak on 22 Jun 2012
Edited: DGM on 31 Dec 2023
Hi
when I am rotating image it get jaggies means edges are beacome zig zag the staircase effect comes can any one have the solution of this problem?

Answers (2)

Image Analyst
Image Analyst on 22 Jun 2012
Try this code to average the jaggies with the background (antialias). Bascially I'm just dividing the perimeter pixels by 2:
clc;
clearvars;
close all;
workspace;
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- 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 in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
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(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% rotate it by 10 degrees
rotatedImage = imrotate(grayImage, 5);
% Display the image.
subplot(2, 2, 2);
imshow(rotatedImage, []);
title('Rotated Image with Jaggies', 'FontSize', fontSize);
% Find the perimeter
binaryImage = bwperim(rotatedImage);
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Perimeter', 'FontSize', fontSize);
% Anti-alias by averaging where the perimeter is only.
noJaggiesImage = rotatedImage; % Initialize
% Now average only where there is a perimeter pixel.
noJaggiesImage(binaryImage) = noJaggiesImage(binaryImage)/2;
% Display the image.
subplot(2, 2, 4);
imshow(noJaggiesImage, []);
title('Jaggies averaged away', 'FontSize', fontSize);
if you want, you can use conv2() to replace the pixel with the average in a bigger window. It's a little more complicated but I'm sure you can handle it.

DGM
DGM on 30 Dec 2023
Edited: DGM on 31 Dec 2023
IPT imrotate() has supported interpolation since at least R2009b.
% an image
% inverted so that the edges have contrast against the padding
% this specific image is logical-class, so make it numeric
% otherwise, there are no intermediate values to interpolate
inpict = im2uint8(~imread('blobs.png'));
% just specify the interpolation
outpict = imrotate(inpict,45,'bilinear');
% show it
imshow(outpict,'border','tight')
% zoom in so that edges are visible on the forum
xlim([50 200])
ylim([85 175])

Categories

Find more on Read, Write, and Modify Image 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!