Tags » Opencv

Detect shapes in an image using opencv.

Let say you have an image like the one above and you wish to find the detect the shapes in the image and find the number of shapes in the image.Then here’s the python code on how to achieve it using opencv library. 156 more words

Programming

Detecting objects in Pac-Man (Mark II)

In my last post, Detecting objects in Pac-Man, I used OpenCV techniques such as Background Subtraction, Contours and colour matching to plot the coordinates of characters in the retro arcade game Pac-Man. 1,532 more words

Technology

How to specify OpenCV color type in Python

Problem

OpenCV has bindings for Python. Specifying the color type in calls to imread or imdecode gives this error:

$ cat foo.py
import cv
import cv2
mat = cv2.imread("foo.png", cv.CV_LOAD_IMAGE_ANYCOLOR)

$ python foo.py
AttributeError: 'module' object has no attribute 'CV_LOAD_IMAGE_ANYCOLOR'
… 111 more words

Air and Paper gets its wings

Yesterday was my day to present the new working prototype of ‘In and Out (Air and Paper)’ to MA-DMA tutor Alex May, and the other students. 47 more words

AGM33 Beyond The Screen: Interactive Installations

Perform Face Detection in Ip Camera using JAVACV


import static com.googlecode.javacv.cpp.opencv_core.CV_AA;
import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;
import static com.googlecode.javacv.cpp.opencv_core.cvClearMemStorage;
import static com.googlecode.javacv.cpp.opencv_core.cvCreateImage;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSeqElem;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSize;
import static com.googlecode.javacv.cpp.opencv_core.cvLoad;
import static com.googlecode.javacv.cpp.opencv_core.cvPoint;
import static com.googlecode.javacv.cpp.opencv_core.cvRectangle;
import static com.googlecode.javacv.cpp.opencv_core.cvSize;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_BGR2GRAY;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_INTER_LINEAR;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCvtColor;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvEqualizeHist;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvResize;
import static com.googlecode.javacv.cpp.opencv_objdetect.CV_HAAR_DO_CANNY_PRUNING;
import static com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.cpp.opencv_highgui;
import com.googlecode.javacv.cpp.opencv_core.CvMemStorage;
import com.googlecode.javacv.cpp.opencv_core.CvRect;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.CvSeq;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_objdetect.CvHaarClassifierCascade;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.io.*;
public class JavaApplication extends JFrame{
public static final String XML_FILE =
"Haar\\haarcascade_frontalface_alt.xml";

public void add(String path) throws Exception {

OpenCVFrameGrabber grabber = new OpenCVFrameGrabber("http://"+path+"//video?dummy=param.mjpg");
grabber.setFormat("mjpeg");
grabber.start();
for (int k=0; k<20000; k++){
System.out.print(k);
}
IplImage src = grabber.grab();
CanvasFrame canvasFrame = new CanvasFrame("Camera IP"+path);
canvasFrame.setCanvasSize(src.width(), src.height());
while (canvasFrame.isVisible() && (src = grabber.grab()) != null) {
// canvasFrame.showImage(frame);
//detect(grabbedImage);
IplImage grayImg = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
cvCvtColor(src, grayImg, CV_BGR2GRAY);

// scale the grayscale (to speed up face detection)
IplImage smallImg = IplImage.create(grayImg.width()/2,
grayImg.height()/2, IPL_DEPTH_8U, 1);
cvResize(grayImg, smallImg, CV_INTER_LINEAR);

// equalize the small grayscale
cvEqualizeHist(smallImg, smallImg);

// create temp storage, used during object detection
CvMemStorage storage = CvMemStorage.create();

// instantiate a classifier cascade for face detection
CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(
cvLoad(XML_FILE));
//System.out.println("Detecting faces...");
CvSeq faces = cvHaarDetectObjects(smallImg, cascade, storage, 1.1, 8,
CV_HAAR_DO_CANNY_PRUNING);

// CV_HAAR_DO_ROUGH_SEARCH);
// 0);
cvClearMemStorage(storage);

// iterate over the faces and draw yellow rectangles around them
int total = faces.total();
// System.out.println("Found " + total + " face(s)");
for (int i = 0; i < total; i++) {
CvRect r = new CvRect(cvGetSeqElem(faces, i));
cvRectangle(src, cvPoint( r.x()*2, r.y()*2 ), // undo the scaling
cvPoint( (r.x() + r.width())*2, (r.y() + r.height())*2 ),
CvScalar.RED, 2, CV_AA, 0);
}
canvasFrame.showImage(src);

// canvasFrame.dispose();
}
grabber.stop();
canvasFrame.dispose();
System.exit(0);

// System.out.println("frame closed");

//System.exit(0);
}
}
…
15 more words
Opencv

How to use OpenCV waitKey in Python

The cv::waitKey(n) function in OpenCV is used to introduce a delay of n milliseconds while rendering images to windows. When used as cv::waitKey(0) it returns the key pressed by the user on the active window. 101 more words

How to use OpenCV constants in Python

OpenCV has a Python API. The Python function calls are well documented and listed along with the C and C++ calls. However, it is not obvious how to use the various OpenCV constants and enums in Python. 38 more words