Ask Your Question
0

measuring height of color using pixel. [closed]

asked 2015-04-04 04:05:11 -0600

Akki gravatar image

updated 2015-04-04 06:24:28 -0600

wuling gravatar image

hello,

I am very new to opencv, I want to measure the height of the coloras in attached figure there is pipette with blood and i want to measure the level of blood in unit like mm. i try with contour but still not get any success to measure the volume. is there any suggestion or code please help me. i tryed also (http://answers.opencv.org/question/15...) but i am getting error.

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv/cxcore.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;


int main(int argc, char** argv) {
VideoCapture stream1(0);   //0 is the id of video device.0 if you have only one camera.

stream1.set(CV_CAP_PROP_FRAME_WIDTH, 640);
stream1.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
int thresh = 100; int max_thresh = 255; RNG rng(12345);

if (!stream1.isOpened()) { 
//check if video device has been initialised
cout << "cannot open camera";
}

//unconditional loop
while (true) {
Mat cameraFrame;
Mat src_gray;
Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

cvtColor( cameraFrame,src_gray, CV_BGR2GRAY ); // produces out2, a one-channel image (CV_8UC1)
    blur( src_gray, src_gray, Size(5,5) );

    threshold( src_gray, threshold_output, 150, 200, CV_THRESH_BINARY_INV);

    findContours( threshold_output, contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    /// Find the rotated rectangles
    vector<RotatedRect> minRect( contours.size() );
for( int i = 0; i < contours.size(); i++ ) 
{ minRect[i] = minAreaRect( Mat(contours[i]) ); }

/// Draw contours + rotated rects + ellipses
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( int i = 0; i < contours.size(); i++ )
{ Scalar color = Scalar( rng.uniform(0,0), rng.uniform(0,0), rng.uniform(250,250) ); // contour drawContours( drawing, contours, i, color, 1, 8, vector<vec4i>(), 0, Point() ); // rotated rectangle    
    Point2f rect_points[4]; minRect[i].points( rect_points );

   for(int j = 0; j < 4; j++ ) 
   line( cameraFrame, rect_points[j], rect_points[(j+1)%4], color, 1, 8 );
 }

    if (stream1.read(cameraFrame))
    {

     namedWindow("cam", 1);
     imshow("cam", cameraFrame);

    }
waitKey(33);

}
return 0;
}

` i want to do measurement using usb camera. Thank you very much in advance for any possible help.

edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by Akki
close date 2015-04-19 08:46:45.550742

Comments

May you can use surface area if you camera is always at same position relative to object.

I find that black surface is 8205 pixels (you must have this value [http://docs.opencv.org/3.0-beta/modul...]) . After you can make a table surface<-> height

LBerger gravatar imageLBerger ( 2015-04-04 07:17:42 -0600 )edit

Could you be a bit more specific? What does happen? What should happen?

FooBar gravatar imageFooBar ( 2015-04-04 07:18:02 -0600 )edit

Thank you for your suggestions, my camera position is fixed, and I have to detect 3 colors at a same time. and i have to measure its level means width.

Akki gravatar imageAkki ( 2015-04-04 16:40:31 -0600 )edit

In your image you haven't got any background so I think you can work with histogram. Histogram integration around your three color will give you region surface. An another idea is to use PCA to find good axis to convert your color image in grayscale instead of cvtColor

LBerger gravatar imageLBerger ( 2015-04-05 02:34:33 -0600 )edit

Hi,Why not use blob or find bounding?????

wuling gravatar imagewuling ( 2015-04-05 08:21:21 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
-1

answered 2015-04-07 05:49:10 -0600

Akki gravatar image

updated 2015-04-07 05:56:47 -0600

theodore gravatar image

hi there,

i want to do detection using usb camera and the problem is that camera frame is not working fine means i can not get continue frame, i tried following code to detect the contour and draw rectangle around it.

#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
#include "stdio.h"
#include "string"
#include <stdlib.h>

using namespace cv;
using namespace std;


int lowerH=168;
int upperH=256;

int lowerS=175;
int upperS=256;

int lowerV=0;
int upperV=256;   

IplImage* frame = 0 ;
IplImage* frame2 = 0 ;
IplImage* imgHSV = 0 ;
IplImage* imgThresh = 0 ;

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;


void draw_conture(IplImage * temp)
{


   cv::Mat tempm(temp,true);  
   findContours( tempm , contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );           
   vector<vector<cv::Point> > contours_poly( contours.size() );
   vector<Rect> boundRect( contours.size() );
   vector<float>area( contours.size() );

   for(  int i = 0; i < contours.size(); i++ )
   {
      approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
      boundRect[i] = boundingRect( Mat(contours_poly[i]) ) ;
      area[i]= contourArea(Mat(contours_poly[i]));

   }

   const CvScalar color=CV_RGB(0x00,0xFF,0x00);                        
   for( int i = 0; i< contours.size(); i++ )
   {                
     if (area[i]>1000)
         rectangle(tempm, boundRect[i].tl(), boundRect[i].br(), color, 1, 1, 0 );                                                      

   }


   CvSeq* contour;  //hold the pointer to a contour
   CvSeq* result;     //hold sequence of points of a contour
   CvMemStorage *storage = cvCreateMemStorage(0); //storage area for all contours

   cvFindContours(temp, storage, &contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));

   for (; contour != 0; contour = contour->h_next)
   {  
       result = cvApproxPoly(contour, sizeof(CvContour), storage, CV_POLY_APPROX_DP, cvContourPerimeter(contour)*0.02, 0);                
       CvRect rect = cvBoundingRect(contour, 0); //extract bounding box for current contour
       if( cvContourArea(result, CV_WHOLE_SEQ,0) >= 1000 )
       {
         cvDrawContours(frame, contour, CV_RGB(0,0,255), CV_RGB(0,0,255), 2, 1, 8, cvPoint(0, 0));
         frame2 = cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels);
         cvCopy(frame, frame2);
         cvRectangle(frame2, cvPoint(rect.x, rect.y), cvPoint(rect.x+rect.width, rect.y+rect.height), CV_RGB(0,0,255),2, 8, 0);  
         cout<< "height =" << rect.height <<endl;
         cout<< "width =" << rect.width <<endl;
         cout<< "x =" << rect.x <<endl;
         cout<< "y =" << rect.y <<endl;

       } 
   }  

   cvShowImage("Object Color Range", frame2);
}
#pragma region trackbar

void on_trackbar1(int position)
{   
    lowerH = position;       
    cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
    cvShowImage("Object Detected", imgThresh);
    draw_conture(imgThresh);   
}

void on_trackbar2(int position)
{
    upperH = position;
    cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
    cvShowImage("Object Detected", imgThresh);
    draw_conture(imgThresh);   
}

void on_trackbar3(int position)
{
    lowerS = position;   
    cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
    cvShowImage("Object Detected", imgThresh);
    draw_conture(imgThresh);
}

void on_trackbar4(int position)
{
    upperS = position;   
    cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
    cvShowImage("Object Detected", imgThresh);
    draw_conture(imgThresh);
}

void on_trackbar5(int position)
{
    lowerV = position;
    cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH,upperS,upperV),imgThresh);
    cvShowImage("Object Detected", imgThresh);
    draw_conture(imgThresh);
}

void on_trackbar6(int position)
{
    upperV = position;
    cvInRangeS(imgHSV, cvScalar(lowerH,lowerS,lowerV), cvScalar(upperH ...
(more)
edit flag offensive delete link more

Comments

why did you return back to the old C unsupported api?

theodore gravatar imagetheodore ( 2015-04-07 05:58:33 -0600 )edit

because capturing video from camera is working some time good with it, so i think to go with it.. what is your suggestion for this, I am new so I have no clear idea about it.

Thank you very much for your reply.

Akki gravatar imageAkki ( 2015-04-07 07:05:42 -0600 )edit

because capturing video from camera is working some time good with it.... that's a really bad excuse. I would definitely recommend you to use the new C++ api.

theodore gravatar imagetheodore ( 2015-04-07 07:11:37 -0600 )edit

thank you for comment. i will try with it. can you suggest me any example code for it to follow.

Akki gravatar imageAkki ( 2015-04-07 07:43:44 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-04-04 04:05:11 -0600

Seen: 583 times

Last updated: Apr 07 '15