1
2
3
4
5
4
3
2
1
0
-1
2
3
4
5
4
3
2
1
0
1
2
3
4
We can visualize these results in GNU Octave with:
samples = load('-ascii', 'samples.txt');
plot(samples);
If we write this code in a script named plot_samples.m, we can simply run plot_samples at the Octave command-line.
]]>The currently committed version takes in a (hard coded) image path and displays the image with a binary threshold, with a check value of 90. In the GUI, there is a slider that allows the user to change the check value to any number between 0 and 255. The image is update in real time. Also, clicking and dragging on the image allows a red line to be drawn across the image. Currently, we limit the user to only one drawing attempt.
The threshold approach works fairly well for lines farther from the center, but quality begins to deteriorate as you move in. We have yet to use the line to calculate the number of rings drawn over and the distances between them.
Here is the current code:
#include <cv.h>
#include <highgui.h>
bool drawingLine,once;
CvPoint p1, p2;
int threshold;
void get_Threshold(IplImage* src, IplImage* dst){
// Allocate individual image planes
IplImage* r = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U, 1);
IplImage* g = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U, 1);
IplImage* b = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U, 1);
//Split image on the color plane
cvSplit(src,r,g,b,NULL);
//Temporary storage
IplImage* s = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
//Add equally weighted rgb values
cvAddWeighted(r, 1./3., g, 1./3., 0.0, s);
cvAddWeighted(s, 2./3., b, 1./3., 0.0, s);
//Threshold
cvThreshold(s,dst,threshold,255,CV_THRESH_BINARY);
cvReleaseImage(&r);
cvReleaseImage(&g);
cvReleaseImage(&b);
cvReleaseImage(&s);
}
void my_mouse_callback(int event, int x, int y, int flags, void* img){
IplImage* image = (IplImage*) img;
switch(event){
//Update endpoint if the line is being drawn. Only allow one line.
case CV_EVENT_MOUSEMOVE:{
if(drawingLine && !once){
p2 = cvPoint(x,y);
}
}
break;
//Set the line points and start drawing as long as we haven't already
//drawn a line
case CV_EVENT_LBUTTONDOWN:{
if(!once){
drawingLine = true;
p1 = cvPoint(x,y);
p2 = cvPoint(x,y);
}
}
break;
//and the line and prevent future lines from being drawn
case CV_EVENT_LBUTTONUP:{
if(!once){
p2 = cvPoint(x,y);
drawingLine = false;
}
once = true;
}
break;
}
}
//update the image threshold from trackbar
void on_change(int pos){
threshold = pos;
}
int main(int argc, char **argv)
{ //initialize
threshold = 90;
IplImage* img = cvLoadImage("C:\\Users\\Corey\\Pictures\\Fish_Research\\FCM060-2fL.jpeg");
IplImage* dst = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U, 1);
drawingLine = false;
once = false;
//Holds the tresholded image
IplImage* thresholdGray = cvCreateImage(cvGetSize(dst),IPL_DEPTH_8U,1);
//holds the color version of the thresholded image so color lines can be drawn on top
IplImage* thresholdColor = cvCreateImage(cvGetSize(dst),IPL_DEPTH_8U,3);
//create the gui
cvNamedWindow("Otolith",0);
cvMoveWindow("Otolith",0,0);
cvResizeWindow("Otolith",cvGetSize(img).width,cvGetSize(img).height);
cvCreateTrackbar("Threshold","Otolith",&threshold,255,on_change);
cvSetMouseCallback("Otolith",my_mouse_callback, (void*)dst);
//run until user hits Esc
while(1){
//threshold the image
get_Threshold(img,thresholdGray);
//create the colored version
cvMerge(thresholdGray,thresholdGray,
thresholdGray,NULL,thresholdColor);
//draw the line if needed
if(drawingLine || once){
cvLine(thresholdColor,p1,p2,CV_RGB(255,0,0),2,8,0);
}
//show the image
cvShowImage("Otolith", thresholdColor);
//check for key press
if(cvWaitKey(15)==27) break;
}
cvReleaseImage(&img);
cvReleaseImage(&dst);
cvReleaseImage(&thresholdGray);
cvReleaseImage(&thresholdColor);
cvDestroyWindow("Otolith");
return 0;
}
]]>
// OpenCVConfiguration.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
int main(int argc, char *argv[])
{
//image loading and allocation to memory
//IplImage* img = cvLoadImage( "S:\\Photos\\Television\\Pokemon\\pokemon.jpg" );
//window creation
//cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
//load image to window
//cvShowImage( "Example1", img );
//wait key causes a pause for a keystroke determined by the parameter
//0 = indefinite wait
//cvWaitKey( 0 );
//without this the window seems to close instantly
//release the memory of the image
//cvReleaseImage( &img );
//deallocate the window memory
//cvDestroyWindow( "Example1" );
//image loading and allocation to memory
IplImage* img = cvLoadImage( "S:\\Photos\\Fish Vision\\FCM060-2FL.jpeg" );
//IplImage* img = cvLoadImage( "S:\\Photos\\Television\\Pokemon\\pokemon.jpg" );
//window creation
cvNamedWindow( "Original", CV_WINDOW_AUTOSIZE );
//load image to window
cvShowImage( "Original", img );
//without this the window seems to close instantly
//Try to grayscale image
//was going to copy image did not work as planned, just going to create a new image
//IplImage* img2 = cvCopy( img );
//does not work requires array does not take iplimage
//cvCvtColor()
//instead create new image to convert to grayscale
//IplImage* img2 = cvCreateImage( cvSize( img->width, img->height ), img->depth, img->nChannels );
IplImage* img2 = cvLoadImage("S:\\Photos\\Fish Vision\\FCM060-2FL.jpeg",CV_LOAD_IMAGE_GRAYSCALE);
//IplImage* img2 = cvLoadImage("S:\\Photos\\Television\\Pokemon\\pokemon.jpg",CV_LOAD_IMAGE_GRAYSCALE);
//cvCvtColor( img, img2, CV_RGB2GRAY );
//window creation
cvNamedWindow( "Grayscale", CV_WINDOW_AUTOSIZE );
//load image to window
cvShowImage( "Grayscale", img2 );
// expects (img, output img, low threshold, high threshold, aperture?) //images must be grayscale
cvCanny( img2, img2, 10, 100, 3 );
//window creation
cvNamedWindow( "Canny Edge Detection", CV_WINDOW_AUTOSIZE );
//load image to window
cvShowImage( "Canny Edge Detection", img2 );
//wait key causes a pause for a keystroke determined by the parameter
//0 = indefinite wait
cvWaitKey( 0 );
//release the memory of the image
cvReleaseImage( &img );
cvReleaseImage( &img2 );
//deallocate the window memory
cvDestroyWindow( "Original" );
cvDestroyWindow( "Grayscale" );
cvDestroyWindow( "Canny Edge Detection" );
return 0;
}
I have left the comments in to show some of my thought process. The next step is to get tortoise svn up and running. I have installed and will be learning how to use it with Cmake.]]>
Here are the steps we take to set up a new Windows machine and a Visual Studio project:
add_executable(my_executable source1.cpp source2.cpp ...)
target_link_libraries(my_executable ${OpenCV_LIBS})
or in Windows Explorer:
The step by step process is listed in the following link: http://siddhantahuja.wordpress.com/2011/07/18/getting-started-with-opencv-2-3-in-microsoft-visual-studio-2010-in-windows-7-64-bit/
I then started to play around with the libraries. I was able to load in an image to a window.
// OpenCVConfiguration.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
int main(int argc, char *argv[])
{
IplImage* img = cvLoadImage( "S:\\Photos\\Television\\Pokemon\\pokemon.jpg" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );
cvWaitKey( 0 );
cvReleaseImage( &img );
return 0;
}
I do not have a webcam on my current computer to test video capture. I am going to attempt to get an IDE for my laptop to try this code. In the next few days I will attempt to code some edge detection for the images sent to me by Professor Johnson.
]]>
sudo apt-get install libcv-dev
did the trick. I tested the installation by writing a short C program to pop up an image:
#include <opencv/highgui.h>
#include <stdio.h>
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: %s path/to/image", argv[0]);
exit(1);
}
char *path = argv[1];
IplImage *img = cvLoadImage(path);
cvNamedWindow(path, CV_WINDOW_AUTOSIZE);
cvShowImage(path, img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow(path);
return 0;
}
To test video playback and webcam capturing, I wrote another little program to play a video frame-by-frame:
#include <iostream>
#include <opencv/highgui.h>
int main(int argc, char **argv) {
CvCapture *movie;
char *path;
// Get frames from movie if one provided. Webcam otherwise.
if (argc > 1) {
movie = cvCreateFileCapture(argv[1]);
path = argv[1];
} else {
movie = cvCreateCameraCapture(0);
path = strdup("camera");
}
cvNamedWindow(path, CV_WINDOW_AUTOSIZE);
IplImage *frame;
// Play movie one frame at a time, advancing only when a non-Escape
// key is pressed. Hit Escape to escape.
char c = '\0';
frame = cvQueryFrame(movie);
while (frame && c != 27) {
cvShowImage(path, frame);
c = cvWaitKey(0);
frame = cvQueryFrame(movie);
}
cvReleaseCapture(&movie);
cvDestroyWindow(path);
return 0;
}
I compiled these at the shell with:
gcc -o image image.cpp -lcv -lhighgui
gcc -o framer framer.cpp -lcv -lhighgui
Installation on Linux was pleasant.
]]>