Edge Detection, Image Manipulation, and Data Extractio

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
rsangole
Posts: 11
Joined: 2014-07-26T13:57:41-07:00
Authentication code: 6789

Edge Detection, Image Manipulation, and Data Extractio

Post by rsangole »

I'm new to this community, and to image processing, but not new to programming (C, VB, Matlab, APDL etc). I'm creating an engineering evaluation tool for a product, that needs to quantify the quality of the edge of a vane. I'll be evaluating hundreds of such vanes using an automated platform with a webcam in exactly the same place each time - thus will be performing statistical image analysis.

Currently, I'm trying this in R, but I'm open to suggestions if it'll make life easier.

An original image is shown here: http://i.imgur.com/q0udvCG.jpg

A hi pass filter later, I get this: http://i.imgur.com/9FoiXe6.jpg

What I need to do:
1. Get the X,Y coordinates of all points on 'Edge 2', that lie between 'Edge 1' and 'Edge 3'. The X,Y origin can be assumed to be the lower left corner of the photo, although, ideally I'd like it to be the intersection of Edge 1 and Edge 2.
2. Store X,Y coordinates in a matrix with an identifier for the vane number. ("Vane 1", "Vane 2", or just 1,2,... )

I'm quite lost on how I proceed from where I am. Since I have no technical education or experience in image processing....

Appreciate any pointers.

Thanks folks

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Edge Detection, Image Manipulation, and Data Extractio

Post by fmw42 »

Here are some ideas. This is how I would approach it. I am not sure this will work for all input image. But it will give you some ideas.

1) Optimize the contrast of the image
2) Extract the best edge image (via canny edge detection)
3) Clean it up (via floodfill and morphology edge)
4) Do the best you can with Hough line detection
5) Processs the Hough line information to find intersections (not detailed below)
6) Convert the edge image to txt format to extact only the x,y coordinates of the white edges
7) Do any further analysis you need from the list of line coordinates. (not detailed below)

Here is an example of the processing.

Optimize the image to contrast stretch it.

Code: Select all

convert q0udvCG.jpg -auto-level q0udvCG_al.png
Image

Do Canny Edge Detection:

Code: Select all

convert q0udvCG_al.png -canny 0x1+10%+30% q0udvCG_al_canny10x30.png
Image

Clean up Canny Edges To Fill as black/white image

Code: Select all

convert q0udvCG_al_canny10x30.png -fill red -draw "color 0,0 floodfill" -alpha off \
-fill black +opaque red -fill white -opaque red q0udvCG_al_binary.png
Image

Re-extract the edge

Code: Select all

convert q0udvCG_al_binary.png -morphology edgein octagon:1 q0udvCG_al_binary_edge.png
Image

Flip the image vertically, so that your coordinates are as if from bottom left corner in the original image
convert q0udvCG_al_binary_edge.png -flip q0udvCG_al_binary_edge_flip.png
Image

Get Hough line image for reference (note there is one extra line due to the fact that your edges were curved)

Code: Select all

convert q0udvCG_al_binary_edge_flip.png -background black -fill white \
-stroke white -strokewidth 1 -hough-lines 25x25+60 q0udvCG_al_binary_edge_flip_hough25x60.png
Image

Get the Hough Line textual information (hough line start, hough line stop, actual edge pixel count, ie length in edge image, not hough line length) and write to mvg text file

Code: Select all

convert q0udvCG_al_binary_edge_flip.png -hough-lines 25x25+60 q0udvCG_al_binary_edge_flip_hough25x60.mvg
http://www.fmwconcepts.com/misc_tests/v ... h25x60.mvg

List the text file to terminal

Code: Select all

cat q0udvCG_al_binary_edge_flip_hough25x60.mvg
# Hough line transform: 25x25+60
viewbox 0 0 594 773
line 0,75.5248 594,117.061  # 66
line 91.8637,0 64.8699,773  # 221
line 81.7219,0 95.2146,773  # 64
line 535.054,0 494.543,773  # 96
line 0,723.84 594,744.583  # 136
Or just put the information in a string and echo to the terminal

Code: Select all

hough_lines=`convert q0udvCG_al_binary_edge_flip.png -hough-lines 25x25+60 MVG:- 2>&1`
echo "$hough_lines"
# Hough line transform: 25x25+60
viewbox 0 0 594 773
line 0,75.5248 594,117.061  # 66
line 91.8637,0 64.8699,773  # 221
line 81.7219,0 95.2146,773  # 64
line 535.054,0 494.543,773  # 96
line 0,723.84 594,744.583  # 136

Extract the individual edge x,y pixel coordinates and put into string (also could write to text file)

Code: Select all

edge_points=`convert q0udvCG_al_binary_edge_flip.png txt:- | grep "white" | sed -n 's/^\([0-9]*,[0-9]*\).*$/\1/p'`
echo "$edge_points"
0,76
1,76
2,76
3,76
4,76
5,76
6,76
7,76
8,76
9,76
10,76
11,76
12,76
13,76
14,76
15,76
16,77
17,77
18,77
19,77
20,77
21,77
22,77
23,77
24,77
...

rsangole
Posts: 11
Joined: 2014-07-26T13:57:41-07:00
Authentication code: 6789

Re: Edge Detection, Image Manipulation, and Data Extractio

Post by rsangole »

Fred,

Many thanks for that. It was more than I expected from a 1st response... much much appreciated.

I had no idea what code you were writing... but figured it was ImageMagick. Then I read up on what this is.. installed it on my mac, and got it working (Barely... can't figure out how to permanently change the PATH and MAGICK_HOME variables; right now, have to do it each time I open a terminal.) Also, trying to figure out how to work with ImageMagick in R.

But, that apart,

The 're-extract the edge' is beautiful. Exactly what I was looking for. But beyond that, looks like we're performing some linear regression fitting for each edge, and extracting end points of the lines? Is that right?

I'm actually looking for all the individual X,Y coordinates on the squiggly edge (edge 2). I don't want a curve fit.. I want the raw data. That'll give me the amount of wear of that edge. (Part starts out straight... squiggles are the wear).

Is this possible? Looks like we are 80% there already...

- Rahul

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Edge Detection, Image Manipulation, and Data Extractio

Post by fmw42 »

The straight lines are not done by fitting. It is a Hough Transform. See http://en.wikipedia.org/wiki/Hough_transform.

I used it only to get the long straight lines from the image so that you could then use the line equations (from the end points at the sides of the image) to compute the intersections you were looking for.

Then I go back to the real edge image and print out a list of all the x,y coordinates of the white pixels that makes up the edges.

You can then do any processing you want on the list of x,y coordinates between the intersections.

You should modify your .profile file to export the path to IM so that you don't have to do it every time.

Check where IM convert was installed. Probably /user/local/bin or /user/bin (unless you installed via MacPorts or something like that)


Add these to the end of your .profile file in your home directory if your path to convert is not listed in your PATH variable. Check by typing in a terminal --- $PATH


Then add these as needed
export PATH="${PATH}:/usr/local/bin # if this is where IM convert resides or put the real path for where it is
export DISPLAY=:0
export TMPDIR=/tmp

and save the file. You may want to make a backup first.

rsangole
Posts: 11
Joined: 2014-07-26T13:57:41-07:00
Authentication code: 6789

Re: Edge Detection, Image Manipulation, and Data Extractio

Post by rsangole »

Ah. Okay. I'll read up on this.

I got the PATHs fixed too, thanks.

The last command to get the X Y points doesn't work for me:

Code: Select all

Rahuls-MacBook-Pro:ImageAnalysis Rahul$ edge_points=`convert edge.png txt:- | grep "white" | sed -n 's/^\([0-9]*,[0-9]*\).*$/\1/p'`
Rahuls-MacBook-Pro:ImageAnalysis Rahul$ echo $edge_points

Rahuls-MacBook-Pro:ImageAnalysis Rahul$ 
echo just turns up blank. Am I doing something wrong?

- Rahul

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Edge Detection, Image Manipulation, and Data Extractio

Post by fmw42 »

type this in a terminal

convert q0udvCG_al_binary_edge_flip.png txt:-

Does the last column show white and black or does it show something like rgb(255,255,255) or gray(255) for white?

What version of IM are you using?

If the above does not show white and black, try

convert q0udvCG_al_binary_edge_flip.png -depth 8 txt:-

Let me know what you are getting?

rsangole
Posts: 11
Joined: 2014-07-26T13:57:41-07:00
Authentication code: 6789

Re: Edge Detection, Image Manipulation, and Data Extractio

Post by rsangole »

Tried the 1st command:

Code: Select all

convert edge.png txt:-
Output:

Code: Select all

467,33: (0,0,0)  #000000  gray(0)
468,33: (0,0,0)  #000000  gray(0)
469,33: (0,0,0)  #000000  gray(0)
470,33: (0,0,0)  #000000  gray(0)
471,33: (0,0,0)  #000000  gray(0)
472,33: (0,0,0)  #000000  gray(0)
473,33: (0,0,0)  #000000  gray(0)
474,33: (0,0,0)  #000000  gray(0)
475,33: (0,0,0)  #000000  gray(0)
476,33: (0,0,0)  #000000  gray(0)
477,33: (0,0,0)  #000000  gray(0)
478,33: (0,0,0)  #000000  gray(0)
479,33: (255,255,255)  #FFFFFF  gray(255)
480,33: (255,255,255)  #FFFFFF  gray(255)
481,33: (255,255,255)  #FFFFFF  gray(255)
482,33: (255,255,255)  #FFFFFF  gray(255)
483,33: (255,255,255)  #FFFFFF  gray(255)
484,33: (255,255,255)  #FFFFFF  gray(255)
485,33: (255,255,255)  #FFFFFF  gray(255)
486,33: (0,0,0)  #000000  gray(0)
487,33: (0,0,0)  #000000  gray(0)
488,33: (0,0,0)  #000000  gray(0)
Version: ImageMagick 6.8.9-3 Q16 x86_64 2014-06-17

rsangole
Posts: 11
Joined: 2014-07-26T13:57:41-07:00
Authentication code: 6789

Re: Edge Detection, Image Manipulation, and Data Extractio

Post by rsangole »

2nd command also results in gray(), not black/white

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Edge Detection, Image Manipulation, and Data Extractio

Post by fmw42 »

Odd that you get gray(255) and not white. I am running IM 6.8.9.5 Q16 Mac OSX Snow Leopard.

Any way this will handle both.

Code: Select all

edge_points=`convert edge.png txt:- | grep "white\|gray(255)" | sed -n 's/^\([0-9]*,[0-9]*\).*$/\1/p'`

P.S. Did you rename the files?

rsangole
Posts: 11
Joined: 2014-07-26T13:57:41-07:00
Authentication code: 6789

Re: Edge Detection, Image Manipulation, and Data Extractio

Post by rsangole »

Yep, that works. How do I save this to a file through the command line?

rsangole
Posts: 11
Joined: 2014-07-26T13:57:41-07:00
Authentication code: 6789

Re: Edge Detection, Image Manipulation, and Data Extractio

Post by rsangole »

P.S. Did you rename the files? --> Yea, I'm using my own filenames...

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Edge Detection, Image Manipulation, and Data Extractio

Post by fmw42 »

Code: Select all

convert edge.png txt:- | grep "white\|gray(255)" | sed -n 's/^\([0-9]*,[0-9]*\).*$/\1/p' > edge_points.txt

Note, once you are happy with the process, it mostly can be combined to be more efficient and not save so many images and or made into a bash shell script.

rsangole
Posts: 11
Joined: 2014-07-26T13:57:41-07:00
Authentication code: 6789

Re: Edge Detection, Image Manipulation, and Data Extractio

Post by rsangole »

Perfect! I've gone from concept-idea in my head, to a working extraction of X Y coordinates within a day, thanks to you! I'll try and use the Hough transform later... but for now, will try to integrate a batch script in R.

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Edge Detection, Image Manipulation, and Data Extractio

Post by fmw42 »

What is R? Ruby?

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Edge Detection, Image Manipulation, and Data Extractio

Post by snibgo »

rsangole wrote:... can't figure out how to permanently change the PATH and MAGICK_HOME variables
Re white/gray(255): perhaps IM can't find colors.xml, which should be in MAGICK_HOME.
snibgo's IM pages: im.snibgo.com

Locked