Simple object recognition

=== solvable ===

Thanks for your suggestions and comments. While working on the flood_fill algorithm in “Starting Python Visualization” (Chapter 9 - Image Processing), I implemented what I wanted. I can count objects, get rectangles for each object (therefore height and width), and finally can build NumPy arrays or matrices for each of them.

Although this is not an optimized approach, it does what I want. The source code (lab2.py) and the png file (lab2-particle.png) that I use were placed under http://code.google.com/p/ccnworks/source/browse/#svn/trunk/AtSc450 .

You need NumPy and PIL, and matplotlib is a histogram. The core of the code lies inside the objfind function, where the action of the main recursive search for the object occurs.

Another update:

SciPy ndimage.label () does exactly what I want.

Cheers for David Ward Farley and Zachary Pincus from the NumPy and SciPy mailing lists to point this out right in the face :)

==============

Hello,

I have an image that contains shadows of ice particles measured by a particle spectrometer. I want to be able to identify each object, so that I can later classify and use them further in my calculations.

In essence, I am ready to do this simply by implementing a fuzzy selection tool, where I can simply select each object.

How can I easily solve this problem? (Preferably using Python)

Thanks.

NOTE. In my question, I mean every specific connected pixel as objects or entities. My intention is to extract them and create NumPy array views, as shown below. (Here I use the top left object, if there is a pixel, use 1 if you do not use 0. This object is 3 by 3, which is 3 pixels high by 3 pixels, respectively. These are projections of real ice particles on a 2D region, under the assumption that they are spherical and equivalent radii (height + width) / 2, and then some scaling will follow - from pixels to the actual size and amount of calculations)

import numpy as np np.array([[1,1,1], [1,1,1], [0,0,1]]) array([[1, 1, 1], [1, 1, 1], [0, 0, 1]]) 

Here is the section from the image that I am going to use.

screenshot http://img43.imageshack.us/img43/2327/particles.png

+6
python image-processing pattern-recognition computer-vision
source share
5 answers
  • Scan each square (e.g. top to bottom, left to right, top to bottom)

  • When you click the blue square, then:

    but. Record this square as the location of the new object.

    b. Find all the other adjacent blue squares (for example, by looking at the neighbors of this square and the neighbors of these neighbors, etc.) and mark them as part of the same object

  • Continue scan

  • When you find another blue square, check if it is part of a known object before proceeding to step 2; alternatively in step 2b, delete any square after you have linked it to the object

+5
source share

If you look at the image that is provided to you, all you have to do is apply a simple algorithm .

If I used MATLAB, I would use bwlabel / bwboundaries . I believe there is an equivalent function somewhere in Numpy, or use OpenCV with python wrappers, as suggested by @kwatford

+3
source share

I used to do this analysis on microphotographs and ended up putting everything I needed into an image processing and analysis package written in C, managed through Tcl. (It only worked with 512 x 512 images, which explains why 512 appear so often. There are images with highlighted pixels of different sizes, but most of the work was done with 8-bit pixels, which explains why there is 0xff and a maximum value of 254 per image.)

In short, "zz" at the beginning of Tcl commands sends the remainder of the line to the packet parser, which calls the corresponding C procedure with the given arguments. Right after "zz" is an argument indicating the input and output of the command. (There may be several inputs, but only one output.) "R" means 512 x 512 x 8-bit image. The third word is the name of the called command; “Graphs” mark the image as described in the text below. So, “zz rr graphs” means “Call the ZZ parser; enter the r-image into the graphs command and return the r-image.” The rest of the Tcl command line indicates which of the pre-selected images to use. (Image “g” is ROI, then is an area of ​​interest, an image, almost all ZZ ops are executed under ROI control.) So, "r1 r1 g8" means "Use r1 as input, use r1 as output (that is, mark the input image) and perform the operation wherever the corresponding pixel in the image is g8 --- that is, r8 used th as ROI --- → 0.

I do not think that it is available on the Internet anywhere, but if you want to select the source code or even compile the whole shebang, I will be happy to send it to you. Here is an excerpt from the manual (but I think I see some errors in the manual at this late date --- this is awkward ...):

Example 6. Counting functions.

Problem

Counting is a common task. The objects being calculated are called “functions”, and it is usually necessary to carefully prepare the images so that the functions correspond to each other with things that are real objects that need to be taken into account. Here, however, we ignore image preparation and instead consider the mechanics of counting. The first counting exercise is to find out how many functions are in the images in the catalog. / cells?

an approach

First, let's define a feature. A feature is the largest group of “set” (non-zero) pixels, all of which can be achieved by moving from one set of pixels to another along routes from north to southeast-west (up-down-right-left), starting from a given pixel recruitment. The zz command, which detects and marks such objects in the image, is a “zz rr-graph R: src R: dest G: ROI”, so-called because the mathematical term for such a function is a “graph”. If all the pixels in the image are installed, then there is only one graph in the image, but it contains 262144 pixels (512 * 512). If the pixels are set and cleared (equal to zero) in the checkerboard template, then there will be graphs 131072 (512 * 512/2), but each will contain only one pixel. It is briefly explained that “zz rr graphs” starts in the upper left corner of the image and scans each following the line from left to right until it finds a fixed pixel, then it finds all installed pixels attached to it through the north, south, east or western borders (“4 -connected "). Then it sets all the pixels in this graph to 1 (0x01). After finding and marking graph 1, it starts scanning again in the pixel after where it first opened graph 1, this time ignoring any pixels that already belong to the graph. The first 254 found graphs will be marked unambiguously; all charts found after this will be marked with the value 255 (0xff) and therefore they cannot be distinguished from each other. The key to accurately counting any number of graphs is to process each image in stages, that is, search for the number of graphs in the image and, if the number is greater than 254, erase only 254 found graphs, repeating until 254 or fewer graphs are found . Tcl provides tools for customizing the management of this operation.

Let's start creating the commands needed to read the ZZ image file into an R-image, and to detect and mark graphs. Before the processing cycle, we declare and nullify the variable to hold the total number of functions in the series of images. Inside the processing cycle, we start by reading the image file into the R-image and detecting and marking the graphs.

 zz ur to $inDir/$img r1 zz rr graphs r1 r1 g8 

We then zero out some variables to track the counts, and then use the "max" command to find out if more than 254 graphs have been detected.

 set nGraphs [ zz ra max r1 a1 g1 ] 

If nGraphs makes equal to 255, then 254 precisely counted graphs should be added to the sum, graphs 1 through 254 should be erased, and the counter should be repeated as many times as needed to reduce the number of graphs below 255.

 while {$nGraphs == 255} { incr sumGraphs 254 zz rbr lt r1 155 r1 g1 0 255 set sumGraphs 0 zz rr graphs r1 r1 g8 set nGraphs [ zz ra max r1 a1 g8 ] } 

When the while loop ends, the nGraphs variable must contain a number less than 255, that is, the number of graphs counted exactly; this adds to the increase in the total number of functions in the series of images.

 incr sumGraphs $nGraphs 

After the processing cycle, print the total number of functions found in the series.

 puts "Total number of features in $inDir \ images $beginImg through $endImg is $sumGraphs." 

After the processing cycle, print the total number of functions found in the series.

+3
source share

OpenCV has a Python interface that may come in handy.

+2
source share

Analysis of connected components may be what you are looking for.

+2
source share

All Articles