Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Assigning sections to elements in image

Subject: Assigning sections to elements in image

From: Johan Mickos

Date: 1 Dec, 2010 19:19:05

Message: 1 of 9

We have a MATLAB project in school where we're supposed to locate transistors in an image of a circuit board, determine their color, and which "section" of the image they're in. The image is 400x680px and is divided into 25 section of 80x136px. It was easy enough to figure out how to determine which section each transistor is in, but I'm wondering if it cold be shortened.
*Note: x and y are vectors containing the coordinates for the centers of the 65 transistors
*Note: the if-statements are part of a for-loop that iterates from i=length(x):-1:1

This is what I have so far. It works, but is very long...

if x(i)>0 && x(i)<136
    elseif x(i)>0 && x(i)<136
        if y(i)>0 && y(i)<80
            section=1;
        elseif y(i)>80 && y(i)<160
             section=6;
        elseif y(i)>160 && y(i)<240
             section=11;
        elseif y(i)>240 && y(i)<320
             section=16;
        else
             section=21;
        end
    elseif x(i)>136 && x(i)<272
        if y(i)>0 && y(i)<80
            section=2;
        elseif y(i)>80 && y(i)<160
             section=7;
        elseif y(i)>160 && y(i)<240
             section=12;
        elseif y(i)>240 && y(i)<320
             section=17;
        else
             section=22;
        end
    elseif x(i)>272 && x(i)<408
        if y(i)>0 && y(i)<80
            section=3;
        elseif y(i)>80 && y(i)<160
             section=8;
        elseif y(i)>160 && y(i)<240
             section=13;
        elseif y(i)>240 && y(i)<320
             section=18;
        else
             section=23;
        end
    elseif x(i)>408 && x(i)<544
        if y(i)>0 && y(i)<80
            section=4;
        elseif y(i)>80 && y(i)<160
             section=9;
        elseif y(i)>160 && y(i)<240
             section=14;
        elseif y(i)>240 && y(i)<320
             section=19;
        else
             section=24;
        end
    else
        if y(i)>0 && y(i)<80
            section=5;
        elseif y(i)>80 && y(i)<160
             section=10;
        elseif y(i)>160 && y(i)<240
             section=15;
        elseif y(i)>240 && y(i)<320
             section=20;
        else
             section=25;
        end
    end

Subject: Assigning sections to elements in image

From: Sean de

Date: 1 Dec, 2010 19:51:05

Message: 2 of 9

"Johan Mickos" <johan.mickos@gmail.com> wrote in message <id6739$87t$1@fred.mathworks.com>...
> We have a MATLAB project in school where we're supposed to locate transistors in an image of a circuit board, determine their color, and which "section" of the image they're in. The image is 400x680px and is divided into 25 section of 80x136px. It was easy enough to figure out how to determine which section each transistor is in, but I'm wondering if it cold be shortened.
> *Note: x and y are vectors containing the coordinates for the centers of the 65 transistors
> *Note: the if-statements are part of a for-loop that iterates from i=length(x):-1:1
>
> This is what I have so far. It works, but is very long...
>
> if x(i)>0 && x(i)<136
> elseif x(i)>0 && x(i)<136
> if y(i)>0 && y(i)<80
> section=1;
> elseif y(i)>80 && y(i)<160
> section=6;
> elseif y(i)>160 && y(i)<240
> section=11;
> elseif y(i)>240 && y(i)<320
> section=16;
> else
> section=21;
> end
> elseif x(i)>136 && x(i)<272
> if y(i)>0 && y(i)<80
> section=2;
> elseif y(i)>80 && y(i)<160
> section=7;
> elseif y(i)>160 && y(i)<240
> section=12;
> elseif y(i)>240 && y(i)<320
> section=17;
> else
> section=22;
> end
> elseif x(i)>272 && x(i)<408
> if y(i)>0 && y(i)<80
> section=3;
> elseif y(i)>80 && y(i)<160
> section=8;
> elseif y(i)>160 && y(i)<240
> section=13;
> elseif y(i)>240 && y(i)<320
> section=18;
> else
> section=23;
> end
> elseif x(i)>408 && x(i)<544
> if y(i)>0 && y(i)<80
> section=4;
> elseif y(i)>80 && y(i)<160
> section=9;
> elseif y(i)>160 && y(i)<240
> section=14;
> elseif y(i)>240 && y(i)<320
> section=19;
> else
> section=24;
> end
> else
> if y(i)>0 && y(i)<80
> section=5;
> elseif y(i)>80 && y(i)<160
> section=10;
> elseif y(i)>160 && y(i)<240
> section=15;
> elseif y(i)>240 && y(i)<320
> section=20;
> else
> section=25;
> end
> end

%Pseudocode
make a binary mask of your transistor
make a labelled image of your sections
sections = unique(section_labelled_image(binary_mask_transistor))


And of course you'll need to loop through transistors storing the results in a cell array or whatever.

Subject: Assigning sections to elements in image

From: Sean de

Date: 1 Dec, 2010 20:02:04

Message: 3 of 9


> %Pseudocode
> make a binary mask of your transistor
> make a labelled image of your sections
> sections = unique(section_labelled_image(binary_mask_transistor))
>
>
> And of course you'll need to loop through transistors storing the results in a cell array or whatever.

You could use this one liner to make your section labelled image, you'll only have to do this once:
section_labelled_image = kron(reshape(1:25,[5 5]),ones(80,136));

And since this class is about image processing, I'm going to assume you know how to mask a specific transistor. Here's pseudocode for looping
%%%
Transistor_Sections = cell(number_of_transistors,1);
for ii = 1:length(Transistor_Sections)
     M = Compute Binary mask of corresponding transistor
     Transistor_Sections{ii} = unique(section_labelled_image(M));
end
%SCd
%%%

Good Luck!

Subject: Assigning sections to elements in image

From: Johan Mickos

Date: 1 Dec, 2010 21:49:06

Message: 4 of 9

Sean de-
I'm afraid I don't know how to mask certain transistors, but I'm sure I'll figure out how if I dig around enough. I just happened to overlook the part of the project directions that states that I'm looking for transistors made up of a 10x10px box with a 4x4 colored square in the center. I thought the best way was to convert the .png into a binary image, manipulate it to fill the centers of the squares, and shrink each shape to a single pixel:

bw=~im2bw(pic,0);
bw2=bwmorph(bw,'thicken',1.01);
bw3=bwmorph(bw2,'fill');
bw4=bwmorph(bw3,'shrink',inf);
[y x]=find(bw4);

Later in the project I'm supposed to recolor each transistor to red(if they failed to work) or white(if they work). I figured that in order to do this, I could just make a copy of the RGB matrix and replace all of the same color around a transistor located on (x,y) with either red or white, depending on their failure rate and location in chip.

I'm still open for other suggestions to accomplish this

Subject: Assigning sections to elements in image

From: ImageAnalyst

Date: 1 Dec, 2010 22:01:36

Message: 5 of 9

Maybe you could upload your image to http://bayimg.com/ so we could
see it.

Subject: Assigning sections to elements in image

From: Johan Mickos

Date: 1 Dec, 2010 22:12:04

Message: 6 of 9

http://image.bayimg.com/mabokaada.jpg



ImageAnalyst <imageanalyst@mailinator.com> wrote in message <f7af0367-c359-4568-b792-dcca0672ab4b@l8g2000yqh.googlegroups.com>...
> Maybe you could upload your image to http://bayimg.com/ so we could
> see it.

Subject: Assigning sections to elements in image

From: Sean de

Date: 1 Dec, 2010 22:29:06

Message: 7 of 9

"Johan Mickos" <johan.mickos@gmail.com> wrote in message <id6h7k$i2i$1@fred.mathworks.com>...
> http://image.bayimg.com/mabokaada.jpg
>

%%%%
% Create Label Mask
%Note the labels are in this order:
% [1 6 11...
% 2 7 12
% 3 8 13
%To change it, transpose the reshape output

Section_Label = kron(reshape(1:25,[5 5]),ones(80,136));

% Read Image
I = imread('mabokaada.jpg');

%Isolate Transistors
Transistors = imfill(rgb2gray(I)<50,'holes');

CC = bwconncomp(Transistors,4); %Enforce 4 connectivity because these are square

%Preallocate Transistor Sections
Transistor_Sections = cell(CC.NumObjects,1);

%Engine:
for ii = 1:CC.NumObjects
    M = false(size(Transistors)); %False Map
    M(CC.PixelIdxList{ii}) = true; %Turn Current Transistor on
    Transistor_Sections{ii} = unique(Section_Label(M)); %Find intersecting Sections
end
%SCd
%%%%

I don't know what you mean by "Failed Transistor." Perhaps you could explain it in more detail. I wouldn't do anything with the original image. Once you know how you want to create the new image, create it using this information - no reason to write over and conflict with old stuff.

Good Luck!

Subject: Assigning sections to elements in image

From: ImageAnalyst

Date: 1 Dec, 2010 22:30:10

Message: 8 of 9

Wow, that makes it a lot easier. Good thing you posted it.

First, threshold to get black - the black squares around each. You
might have to extract the color planes and do some combining of the
individually thresholded color planes.
Then do a hole fill with imfill.
Then label this binary image.
Then extract each color plane.
Pass each color plane along with the labeled image in to regionprops
to get the centroids and intensities of each transistor in each color
plane (in other words the color).
That's basically it. Since it's your assignment you'll have to fill
out the details but it's really not many lines of code at all.

You might look at my image segmentation demo if you need examples:
http://www.mathworks.com/matlabcentral/fileexchange/25157

Subject: Assigning sections to elements in image

From: Johan Mickos

Date: 2 Dec, 2010 00:18:04

Message: 9 of 9

Thanks to both of you! The course I'm taking is only introduction to MATLAB, so much of this isn't even in our book, but I love learning of new methods.Time to start working!
//Johan Mickos

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us