Please Note: All the images are scaled down to fit in the pdf.

Problem 5)

Identifying 30deg oriented edges

I = imread('triangle.gif');

imshow(I);

hx = [1 2 1; 0 0 0; -1 -2 -1];

hy = [1 0 -1; 2 0 -2; 1 0 -1];

theta = pi/6;

h = hx*cos(theta) * hy*sin(theta);

I2 = imfilter(I,h);

imshow(I2)

Input Image

Output Image

triangle.png

output.png

Problem 6)

Identifying four corners of rectangle

I = imread('rect.png');

%{ LR %}

hlr = [-1, -1,  1; -1,  0,  0; 1,  0,  -1];

%{ LL %}

hll = [1, -1, -1; 0,  0,  -1; -1,  0, 1];

%{ UL %}

hul = [-1,  0,  1; 0,  0,  -1; 1, -1, -1];

%{ UR %}

hur = [1,  0, -1; -1,  0,  0; -1, -1,  1];

ur = imfilter(I,hur);

ul = imfilter(I,hul);

ll = imfilter(I,hll);

lr = imfilter(I,hlr);

imshow(ur + ul + ll + lr);

Input Image

Output Image

rect.png

output.png

Problem 7)

Finding rectangles

a = imread('checkboard.png');

I2 = a;

a = rgb2gray(a);

[m, n] = size(a);

I = a;

% thresholding the image %

for i=1:m

        for j=1:n

            if a(i,j)<=128

                I(i,j) = 0;

            else

                I(i,j) = 255;

            end

        end

end

% softening the image %

h = [1, 1, 1; 1, 1, 1; 1, 1, 1;];

h = h * 1/4;

I = imfilter(I,h);

hlr = [-1, -1,  1; -1,  0,  0; 1,  0,  -1]; %{ LR %}

hll = [1, -1, -1; 0,  0,  -1; -1,  0, 1];   %{ LL %}

hul = [-1,  0,  1; 0,  0,  -1; 1, -1, -1];  %{ UL %}

hur = [1,  0, -1; -1,  0,  0; -1, -1,  1];  %{ UR %}

ul = imfilter(I,hul);

ur = imfilter(I,hur);

ll = imfilter(I,hll);

lr = imfilter(I,hlr);

[h,w] = size(I);

rect_exists = 0;

for i=1:w

        for j=1:h

            rect_exists = 0;

            if (ul(j, i) > 0) % ul is found %

                ulx = i;

                uly = j;

               

                for i2=ulx:w

                    if (ur(j, i2) > 0) % ur is found %

                        urx = i2;

                        ury = j;

                       

                        for j1=j:h

                            if (lr(j1, urx) > 0) % lr is found %

                                lrx = urx;

                                lry = j1;

                               

                                for i3=lrx:-1:1

                                    if (ll(lry, i3) > 0) % ll is found %

                                        llx = i3;

                                        lly = lry;

                                       

                                        if (llx == ulx) % ul and ul intersects%

                                           rect_exist = 1;

                                               hold on;

                                               I2 = imshow(I2);

line([ulx,urx],[uly,ury],'Color','r','LineWidth',2)

line([urx,lrx],[ury,lry],'Color','r','LineWidth',2)

line([lrx,llx],[lry,lly],'Color','r','LineWidth',2)

line([llx,ulx],[lly,uly],'Color','r','LineWidth',2)

                                               hold off

                                        end

                                    end

                                end

                                break;

                            end

                        end

                    end

                end

            end

        end

end

Input Image

Output Image

checkboard.png

checkerboard-solution.png

Input Image

Output Image

b3.png

building-solution.png

Problem 9)

Histogram Equivalization

I = imread('appa-cora.jpg');

[h,w,z] = size(I);

totPixels = h * w;

%{ Counts the frequency of each intensity %}

f_arr = zeros(1, 256);

for i=1:h

        for j=1:w

            temp = I(i,j);

            f_arr(temp + 1) = f_arr(temp + 1 ) + 1;

        end

end

%{ probability of each intensity %}

p_arr = zeros(1, 256);

for i=1:256

   p_arr(i) = f_arr(i)/totPixels;

end

%{ cummulative frequency distribution function %}

cdf = zeros(1, 256);

cdf(1) = p_arr(1);

for i=2:256

        cdf(i) = cdf(i-1) + p_arr(i);

end

%{ cdf scaled %}

cdf_scaled = zeros(1, 256);

for i=1:256

        cdf_scaled(i) = round(cdf(i) * 255);

end

I2 = zeros(h, w);

%{ I2 = new image with intensity replaced with scaled values %}

for i=1:h

        for j=1:w

            I2(i, j) = cdf_scaled(I(i, j) + 1);

        end

end

I2 = uint8(I2);

imshow(I);

title('Original Image')

figure,imshow(I2);

title('Histogram Equivalized Image')

Input Image

Output Image

original-image.png

histogram-equivalized-image.png

Program 10)

Stretching an image

I = imread('circle.png');

[x, y, z] = size(I);

a = 1;

b = 4;

c = 2;

d = 2;

e = 1;

f = 1;

I2 = zeros(x*2, y*2);

for i=1:x

        for j=1:y

            new_x = a*i + b*j + c;

            new_y = d*i + e*j + f;

            I2(new_y, new_x) = I(j, i);

        end

end

figure, imshow(I);

figure, imshow(I2);

Input Image

Output Image (a=1, b=4, c=2, d=2, e=1, f=1)

circle.png

stretched circle.png