c++ - Opencv - Finding edges of different objects with same algorithm -


i pretty new opencv , first project, need suggestions. trying find dimension of different objects. have written code based on tutorials on opencv.org , examples on learning opencv book bradski & kaehler.

until working 1 object. when tried find dimension of different object, realized needed change threshold1 value of canny.

so question is: how can make program kind of adjustments different objects automatically? won't need change manually. kind of algorithm should use?

background same, camera's position fixed, can assume lighting same.

for picture need threshold1 value of 53 in order find edges. bluepaper2.png

for one, value of 141 works pretty good. harddisk2.png

i couldn't find values one, since color same background's. bigpaper1.png

here code:

#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> #include <stdio.h> #include <stdlib.h>  using namespace cv; using namespace std;  mat src, src_gray, src_blur; mat canny_output;  int canny_thresh = 53; int canny_max_thresh = 200; int canny_kernel = 3; int canny_ratio = 3; bool l2gradient = true;  char* source_window = "source"; char* canny_window = "canny"; char* contour_window = "contours";  /** @function main */ int main(int argc, char** argv) {     /// load source image , convert gray     src = imread("bluepaper2.png", 1);      /// create window        namedwindow(source_window, cv_window_autosize);     imshow(source_window, src);      /// convert image gray , blur     cvtcolor(src, src_gray, cv_bgr2gray);      /// reduce noise kernel 3x3     blur(src_gray, src_blur, size(3, 3));         /// detect edges using canny     canny(src_blur, canny_output, canny_thresh, canny_thresh * canny_ratio, canny_kernel, l2gradient);      vector<vector<point> > contours;     vector<vec4i> hierarchy;      /// find contours     findcontours(canny_output, contours, hierarchy, cv_retr_tree, cv_chain_approx_simple, point(0, 0));      /// find rotated rectangles , ellipses each contour     vector<rotatedrect> minrect(contours.size());     vector<rotatedrect> minellipse(contours.size());      (int = 0; < contours.size(); i++)     {         minrect[i] = minarearect(mat(contours[i]));         if (contours[i].size() > 5)         {             minellipse[i] = fitellipse(mat(contours[i]));         }     }      /// draw contours + rotated rects + ellipses     mat drawing = mat::zeros(canny_output.size(), cv_8uc3);     (int = 0; i< contours.size(); i++)     {         // contour         drawcontours(drawing, contours, i, scalar(255, 255, 0), 2, 8, vector<vec4i>(), 0, point());         // ellipse         ellipse(drawing, minellipse[i], scalar(0, 0, 255), 2, 8);         // rotated rectangle         point2f rect_points[4]; minrect[i].points(rect_points);         (int j = 0; j < 4; j++)             line(drawing, rect_points[j], rect_points[(j + 1) % 4], scalar(0, 255, 0), 2, 8);     }      /// show in window     namedwindow(canny_window, cv_window_autosize);     namedwindow(contour_window, cv_window_autosize);      imshow(canny_window, canny_output);     imshow(contour_window, drawing);      waitkey(0);     return(0); } 

thanks in advance.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -