CMPS-4350 MATLAB Image Processing Lab

Work with Image Processing ToolBox on MATLAB.

Setup: Download MATLAB 2025 here SIGN UP USING YOUR CSUB EMAIL SO YOU DONT HAVE TO PAY Goal: Find circle, rectangle, triangle objects in an image Goal2: Create your own image for testing
MATLAB Basic Commands Original Tutorial: Identifying Round Objects
Find Circular Objects In An Image: Setup: Download findShapes.m Setup: Download findCircle.m "pillsetc.png": Image Provided By MATLAB Step 1: Preprocess the Image 1. read image, display, and save to variable >> colored_image = imread("pillsetc.png"); >> displayImage(colored_image, "Original Image") 2. convert rgb image to a binary image >> grayed = im2gray(colored_image); >> bw = imbinarize(grayed); >> displayImage(bw, "Black and White Image") 3. remove extra pixels and fix empty space >> minSize = 30; >> bw = bwareaopen(bw,minSize); >> displayImage(bw, "Filtered Image") >> se = strel("disk",2); >> bw = imclose(bw,se); % fill small gaps and holes >> displayImage(bw, "Unfilled Fixed Image") >> bw = imfill(bw,"holes"); % fill any holes in objects >> displayImage(bw, "Filled Fixed Image") 4. find boundaries of objects >> [B,L] = bwboundaries(bw,"noholes"); >> labeled_image = label2rgb(L,@jet,[.5 .5 .5]); >> displayImage(labeled_image, 'Objects with Boundaries in White') >> hold on >> for k = 1:length(B) boundary = B{k}; plot(boundary(:,2),boundary(:,1),"w",LineWidth=2) >> end Step 2: Find Circles In Image 1. get information of the objects >> stats = regionprops(L, "Circularity", "Centroid"); 2.: Set threshold >> threshold = 0.94; 3.: Loop through objects to determine shape >> for k = 1:length(B) boundary = B{k}; circle_value = stats(k).Circularity; if circle_value > threshold shape = "Circle"; end centroid = stats(k).Centroid; text(boundary(1, 2)-35, boundary(1, 1)+13, shape, Color="y", FontSize=14, FontWeight="bold") end >> title("Identified Shapes")
Task 1: Make A Custom Image In MATLAB 1. Create a new script named "drawShapeImage" 2. Create a blank image This can be done using zeros function it will have 4 parameters 1. size of the x axis 2. size of the y axis 3. rgb color channels (how many channels are there in a rgb image?) 4. unsigned int typename You can create any shape but this is to test the image processing The insertShape function will be used to do this 3. Draw a Rectangle call insertShape with a total of 5 parameters and save to img 1. image you want to insert the shape 2. filled shape name 3. dimensions, [x1, y1, width, height] 4. 'Color' (this one stays the same) 5. name or rgb value of color you want 4. Draw a Circle call insertShape with a total of 5 parameters and save to img 1. image you want to insert the shape 2. filled shape name 3. dimensions, [x, y, radius] 4. 'Color' (this one stays the same) 5. name or rgb of color you want 5. Draw a Triangle make a variable called "triangle" set it equal to a matrix with each verticies, [x1, y1; x2, y2; x3, y3] call insertShape with a total of 5 parameters and save to img 1. image you want to insert the shape 2. filled shape name (wont be triangle) 3. triangle variable 4. 'Color' (this one stays the same) 5. name or rgb of color you want 6. Display And Save The Image call imshow function with img as a parameter under imshow, put "axis on" to better visual the points call imwrite function to save img to a png Sample Image Created Using This Method:
Task 2: Find Rectangles And Triangles In An Image Setup: the "findCircle.m" from above will be used for this section As all the image setup is done already, you can start from line 56 1. Add more parameters to the regionprops function The regionprops function is a function from the Image Processing Toolbox where it measures several properties from objects in an image The total parameters for the regionprops function will be 5 We already know 3 of these parameters: "L", "Circularity", "Centroid" The 1st parameter will be the one that uses eccentricity for triangle The 2nd parameter will be the one that uses solidity for rectangle 2. Set threshold for each shape The threshold for all shapes will be between 0 and 1 We already have the threshold for a circle being 0.96 To get the threshold for a triangle and rectangle, threshold for triangle that I used "0.6" threshold for rectangle that I used "0.9" 3. Add more logic to the inside of the for loop We will want to set the values from "stats" saved similar to "circle_value = stats(k).Circularity;" After this we will update the if else section to be an if elseif else section >> if circle_value > threshold_circularity >> shape = "Circle"; >> elseif regionpropvalue < threshold_triangle >> shape = "Triangle"; >> elseif regionpropvalue > threshold_rectangle >> shape = "Rectangle"; >> else >> shape = "Other"; >> end Setup: Download findShapes.m Setup: Download findShapes.m Setup: Download findShapes.m Setup: Download findShapes.m Setup: Download findShapes.m Sample Run Of Finished Lab :