Please Note: My code is working, however it is currently offset to the bottom le
ID: 3588045 • Letter: P
Question
Please Note:
My code is working, however it is currently offset to the bottom left corner. I think I am doing the math wrong but I dont know for sure. Thanks!
Write a program that turns a rectangular color image into a round black and white image.
Here's an input image:
The images given for this lab are: the same queen-mary.gif that is in module 3 exercise, and a jellyfish.gif shown above. They're in the lab3.zip file.
Suggested steps for your program
Read in the image.
Find the height and the width of the image. Then use the smaller of the height and width to calculate the radius of the output image. In the example above, the height is smaller than the width, therefore the circular image has the same diameter as the height.
Walk through each pixel of the image, and for each pixel that's:
- farther from the center of the image than the radius, set the color of the pixel to white.
The formula for calculating distance of a pixel from the center of the image is:
(Regular distance fromula from math)
where (x1,y1) is the current location of the pixel and (x2, y2) is the center location of the image
- closer or equal to the radius, set the color of the pixel to gray.
The formula for calculating the grayscale value of a pixel is:
gray = 0.2126 x red + 0.7152 x green + 0.0722 x blue
where red, green, blue are the RGB values of the pixel.
Once the grayscale value is calculated, store it in place of the red, green, and blue values of the pixel.
Draw the image.
THANKS!!
CIS 41A - Lab 3: loops and if statements This assignment is a combination of textbook P4.55 and P4.52 Write a program that turns a rectangular color image into a round black and white image Here's an input image and the output image The images given for this lab are: the same queen-mary.gif that is in module 3 exercise, and a jellyfish.gif shown above. Theyre in the lab3.zip file. Suggested steps for your program 1. Read in the image. 2. Find the height and the width of the image. Then use the smaller of the height and width to calculate the radius of the output image. In the example above, the height is smaller than the width, therefore the circular image has the same diameter as the height. 3. Walk through each pixel of the image, and for each pixel that's: - farther from the center of the image than the radius, set the color of the pixel to white The formula for calculating distance of a pixel from the center of the image is where (xi,y) is the current location of the pixel and (x2, y2) is the center location of the mage closer or equal to the radius, set the color of the pixel to gray. The formula for calculating the grayscale value of a pixel is: gray = 0.2126 x red + 0.7152 x green + 0.0722 x blue where red, green, blue are the RGB values of the pixel. Once the grayscale value is calculated, store it in place of the red, green, and blue values of the pixel 4. Draw the image.Explanation / Answer
%Rectangle2CircularGray.m
clear;
clc;
image=double(imread('j.jpg'));%filename
[row,col,c]=size(image);
dia=min(row,col);
radius=dia/2;
dia=int32(dia);
cX=int32(row/2);%center X
cY=int32(col/2);%center Y
new=zeros(dia,dia,3);%output image
%instead of making it RGB, could make new image directly of 1 array of colors,grayscale
%new=zeros(dia,dia);
x=cX-radius;
%process only those pixels in the input image which need to be there in the output,so start from cX-radius, and will
%go till cX-radius+dia, i.e. cX+radius, and same for y
for i=1:dia
y=cY-radius;
for j=1:dia
if(getDist(double(x),double(y),double(cX),double(cY))<radius)
%getDist function is in another file, getDist.m, it calculates the distance between pixels, file is at the end
value=(image(x,y,1)*.2126)+(.7152*image(x,y,2))+(.0722*image(x,y,3));
new(i,j,1)=value;
new(i,j,2)=value;
new(i,j,3)=value;
%instead of above 4 statements, could also do this
%new(i,j)=(image(x,y,1)*.2126)+(.7152*image(x,y,2))+(.0722*image(x,y,3));
else
new(i,j,1)=255;
new(i,j,2)=255;
new(i,j,3)=255;
%instead of these 3 statements
%new(i,j)=255;
end
y=y+1;
end
x=x+1;
end
imshow(uint8(new));
%------------------------------------------------------getDist.m------------------------------------------------------------------------------------
%getDist.m
function[D]=getDist(x1,y1,x2,y2)
sq1=(x2-x1)^2;
sq2=(y2-y1)^2;
D=sqrt(sq1+sq2);
end