All About Circuits Forum Code for image comparison in MATLAB pt. 2
 Register Blogs FAQ Members List Today's Posts Search Today's Posts Mark Forums Read

 Programmer's Corner Discussion forum for all aspects of programming and software engineering. Any software programming language welcome: C, C++, C#, Fortran, Java, Matlab, etc.

#1
04-27-2008, 04:38 PM
 Tek_Knowledge New Member Join Date: Apr 2008 Posts: 6
Code for image comparison in MATLAB pt. 2

All right, so i've developed my algorithm for spectral comparison in MATLAB. I read the images into matlab and convert them to grayscale. Now what i want to do have the program scan the images for black pixels. so basically

numbkpixels = 0;
while (in picture range)
if pixel = black
numbkpixels = numbxpixels + 1
else
go to next pixel
return numbkpixels

any ideas?

Last edited by Tek_Knowledge; 04-27-2008 at 05:45 PM.
#2
04-27-2008, 06:44 PM
 Dave Circuits Santa Join Date: Nov 2003 Location: United Kingdom (GMT) Posts: 6,959 Blog Entries: 17

If at all possible you need to avoid using loops in Matlab, particularly for large image sets. Can I ask what function (or argument from your image read function) you using to obtain the grayscale image? The reason I ask is that there might be a vectorised option built into Matlab, but this will depend on the functions used.

Dave
__________________

"If I have seen a little further it is by standing on the shoulders of Giants" - Sir Isaac Newton
#3
04-27-2008, 06:55 PM
 Tek_Knowledge New Member Join Date: Apr 2008 Posts: 6

Quote:
 Originally Posted by Dave If at all possible you need to avoid using loops in Matlab, particularly for large image sets. Can I ask what function (or argument from your image read function) you using to obtain the grayscale image? The reason I ask is that there might be a vectorised option built into Matlab, but this will depend on the functions used. Dave

function
rgb2gray
#4
04-27-2008, 11:12 PM
 Dave Circuits Santa Join Date: Nov 2003 Location: United Kingdom (GMT) Posts: 6,959 Blog Entries: 17

I cannot see a vectorisation option in Matlab for the output of the rgb2gray function so we are looking at loops.

Am I correct to assume the grayscale image is of datatype uint8? (You will be able to see this from the Matlab workspace). The important point is that datatype is unsigned, therefore the pure-black pixels will be of value 0.

What you are asking for is a function that will deduce the number of black pixels. If so, then the following loops will work (be sure to debug the syntax - I am writing this without Matlab to hand):

Code:

J = rgb2gray(I);

numblkpxl = 0;
for n = 1:(size(J,2))
for m = 1:(size(J,1))
if isequal(0,J(m,n))
numblkpxl = numblkpxl + 1;
end
end
end
Select All
There is an important point to note here: depending on the formatting of your original image, when the grayscale thresholding is performed through rgb2gray, the deduction of the black intensity might not be exactly 0 - i.e. it may for all intents purposes be black on the original; however the thresholding pitches the grayscale intensity at a value of 1 or 2.

Dave
__________________

"If I have seen a little further it is by standing on the shoulders of Giants" - Sir Isaac Newton

 Tags code, comparison, image, matlab

 Similar Threads Thread Thread Starter Forum Replies Last Post zico Programmer's Corner 6 01-29-2013 04:10 PM Dave Programmer's Corner 5 04-28-2009 11:20 PM ninjutsu Programmer's Corner 3 03-14-2009 07:59 PM Dave Programmer's Corner 7 11-09-2008 06:51 AM Dave Programmer's Corner 0 01-22-2006 02:07 PM

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Electronics Forums     General Electronics Chat     The Projects Forum     Homework Help     Electronics Resources Software, Microcomputing, and Communications Forums     Programmer's Corner     Embedded Systems and Microcontrollers     Computing and Networks     Radio and Communications Circuits and Projects     The Completed Projects Collection Abstract Forums     Math     Physics     General Science All About Circuits Commmunity Forums     Off-Topic     The Flea Market     Feedback and Suggestions

All times are GMT. The time now is 11:17 PM.