All About Circuits Forum  

Go Back   All About Circuits Forum > Software, Microcomputing, and Communications Forums > Programmer's Corner

Notices

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.

Reply   Post New Thread
 
Thread Tools Display Modes
  #1  
Old 04-27-2008, 03:38 PM
Tek_Knowledge Tek_Knowledge is offline
New Member
 
Join Date: Apr 2008
Posts: 6
Default 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 04:45 PM.
Reply With Quote
  #2  
Old 04-27-2008, 05:44 PM
Dave Dave is offline
Senior Member
 
Join Date: Nov 2003
Posts: 6,961
Blog Entries: 17
Default

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
Reply With Quote
  #3  
Old 04-27-2008, 05:55 PM
Tek_Knowledge Tek_Knowledge is offline
New Member
 
Join Date: Apr 2008
Posts: 6
Default

Quote:
Originally Posted by Dave View Post
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
Reply With Quote
  #4  
Old 04-27-2008, 10:12 PM
Dave Dave is offline
Senior Member
 
Join Date: Nov 2003
Posts: 6,961
Blog Entries: 17
Default

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:
% I is your image from imread or other image read function

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
Reply With Quote
Reply   Post New Thread

Tags
, , ,


Similar Threads
Thread Thread Starter Forum Replies Last Post
Matlab code needed!! zico Programmer's Corner 6 01-29-2013 03:10 PM
Matlab R2007 stuck initializing Dave Programmer's Corner 5 04-28-2009 10:20 PM
Matlab Source Code (discrete wavelet transform) ninjutsu Programmer's Corner 3 03-14-2009 06:59 PM
Useful Matlab Information Dave Programmer's Corner 7 11-09-2008 05:51 AM
Installing Matlab Student on Suse Linux 10 Dave Programmer's Corner 0 01-22-2006 01:07 PM

Thread Tools
Display Modes

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 Jump


All times are GMT. The time now is 04:56 PM.


User-posted content, unless source quoted, is licensed under a Creative Commons Public Domain License.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.