marco
marco

Reputation: 87

Manipulate color within a circle in image with MATLAB

I'm changing the color saturation and other perks in an image using matlab. I already done this.

close all
clear all
clc
A = imread('fractalHD.png');
B = A;
[f,c,p] = size(A);
R = uint8(zeros(f,c,p));
Value = 120;

for i=1:round(f/2) 
for j=1:round(c) 
    for k=1:p
    R(i,j,k) = B(i,j,k) - Value;
    end
end
end

for i=round(f/2):f
for j=1:round(c)
    for k=1:p
    R(i,j,k) = B(i,j,k);    
    end
end
end    

figure; imshow(R);

This takes the superior half of an image and reduces color intensity by a value. I want to do this but, instead of the superior half (or any square shaped part), do it within a circle with center at top left.

rad = 500;
theta = 0:2*pi/10000:2*pi;

for i=1:rad*cos(theta)
for j=1:rad*sin(theta)
    for k=1:p
    R(i,j,k) = B(i,j,k) - value;
    end
end
end
imshow(R);

Using this code only returns a black image. Any sugestions?

Upvotes: 0

Views: 63

Answers (1)

zeeMonkeez
zeeMonkeez

Reputation: 5157

This is how I'd do it, without loops but with bsxfun:

% Center and radius of circle
center = [200, 600];
rad = 500;

% determine which coordinates are within circle (returns a logical array)
indmat = bsxfun(@(row,col) sqrt((row - center(1)).^2 + (col-center(2)).^2) <= rad, (1:f)', 1:c);
% scalar multiply to get double array, convert to uint8, and subtract with
% singleton expansion (along dimension 3)
R = bsxfun(@minus, B, uint8(indmat * Value)) ;

figure; imshow(R);

The problem with those for loops is that the outer one computes to 1:500 and the second one to 1:0 (because theta itself is a vector and 1:vector is equivalent to 1:vector(1). So nothing gets ever computed. If you really wanted to do it with loops you'd have to go 1:2*rad in the outer loop and determine the length of the inner loop appropriately, for example

R = B;
for ii=1:2*rad
    for jj=-round(sqrt((rad)^2 - (rad-ii)^2)):round(sqrt((rad)^2 - (rad-ii)^2))
        for k=1:p
            R(ii,jj+rad+1,k) = R(ii,jj+rad+1,k) - Value;
        end
    end
end

But that's rather ugly, and extra tests would be necessary if the circle is not completely contained in the image.

Upvotes: 2

Related Questions