简体   繁体   中英

How to convert this code into javacv?

I had went through many tutorials and guide lines and I was able to convert part of it. But I don't know how to convert last line in to javacv. So Please can some one help me to convert this code in to javacv?

img = cv2.imread('sofud.jpg')

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

ret,thresh = cv2.threshold(gray,127,255,1)

contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

  for cnt in contours:
    x,y,w,h = cv2.boundingRect(cnt)
    if 10 < w/float(h) or w/float(h) < 0.1:
    cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)

You can use the last line as this in Java CV:

opencv_core.cvRectangle(image, opencv_core.cvPoint(m,n), opencv_core.cvPoint(i,j), opencv_core.cvScalar(0,0,255,0), thickness, 8, 0);

Where the parameters mean:

opencv_core.cvRectangle(CvArr* img, CvPoint , CvPoint, CvScalar color, int thickness=1, int lineType=8, int shift=0)

and look at this Link for more information.

EDIT:

This is an example of finding contours:

cvFindContours(grayImage, memory, contours, sizeof(CvContour.class) , CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));

Where the parameter mean:

cvFindContours(CvArr* image, CvMemStorage* storage, CvSeq** first_contour, int header_size=sizeof(CvContour), int mode=CV_RETR_LIST, int method=CV_CHAIN_APPROX_SIMPLE, CvPoint offset=cvPoint(0,0) )

For more information on the parameters, take a look at this Link , under FindContours section.

You can also use the cvDrawContours function, to draw the contours in the same way as we have used cvFindContours function.

Take a look at this Link . It is a different post on this website in which the they have explained the use of finding contours in blob detection.

Hope This Helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM