我不是在CPP好,我需要有這樣的代碼的C接口:OpenCV的 - 如何轉換一段cpp的代碼用C
#include <cv.h>
#include <highgui.h>
#include <vector>
int main(int argc, char* argv[])
{
cv::Mat img = cv::imread(argv[1]);
// Convert RGB Mat to GRAY
cv::Mat gray;
cv::cvtColor(img, gray, CV_BGR2GRAY);
// Store the set of points in the image before assembling the bounding box
std::vector<cv::Point> points;
cv::Mat_<uchar>::iterator it = gray.begin<uchar>();
cv::Mat_<uchar>::iterator end = gray.end<uchar>();
for (; it != end; ++it)
{
if (*it) points.push_back(it.pos());
}
// Compute minimal bounding box
cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));
// Draw bounding box in the original image (debug purposes)
//cv::Point2f vertices[4];
//box.points(vertices);
//for (int i = 0; i < 4; ++i)
//{
//cv::line(img, vertices[i], vertices[(i + 1) % 4], cv::Scalar(0, 255, 0), 1, CV_AA);
//}
//cv::imshow("box", img);
//cv::imwrite("box.png", img);
// Set Region of Interest to the area defined by the box
cv::Rect roi;
roi.x = box.center.x - (box.size.width/2);
roi.y = box.center.y - (box.size.height/2);
roi.width = box.size.width;
roi.height = box.size.height;
// Crop the original image to the defined ROI
cv::Mat crop = img(roi);
cv::imshow("crop", crop);
cv::imwrite("cropped.png", crop);
cvWaitKey(0);
return 0;
}
有人可以幫我把它包起來或轉換嗎? 謝謝!!
編輯:
這是我嘗試:
IplImage *digit,*gray,*thresh;
digit = cvLoadImage("digit.png", 1);
gray = cvCreateImage(cvGetSize(digit), digit->depth, 1);
thresh = cvCreateImage(cvGetSize(digit), digit->depth, 1);
cvCvtColor(digit, gray, CV_RGB2GRAY);
cvThreshold(gray, thresh, 250, 255, CV_THRESH_BINARY);
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* ptseq = cvCreateSeq(CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvContour),sizeof(CvPoint),storage);
int i,j;
CvPoint point;
for(i=0;i<thresh->width;i++){
for(j=0;j<thresh->height;j++){
if(cvGet2D(thresh, j, i).val[0]==0){
point.x=i;
point.y=j;
cvSeqPush(ptseq, &point);
}
}
}
CvRect box = cvBoundingRect(ptseq, 1);
CvRect roi;
roi.x = box.x - (box.width/2);
roi.y = box.y - (box.height/2);
roi.width = box.width;
roi.height = box.height;
cvSetImageROI(thresh, roi);
IplImage *result = cvCreateImage(cvGetSize(thresh), thresh->depth, 1);
cvCopy(thresh, result,NULL);
cvResetImageROI(thresh);
cvShowImage("output", result);
cvWaitKey(0);
cvDestroyAllWindows();
return 0;
對於那些誰不知道,這個代碼是在[另一個答案]我寫的(http://stackoverflow.com/a/10317919/176769)。我認爲這個問題實際上是家庭作業(而不是一個好的)是公平的。你可以再詳細一點嗎?代碼中的哪些部分是你正在努力的? – karlphillip 2012-04-25 22:34:23
std ::向量分; cv :: Mat_ :: iterator it = gray.begin (); cv :: Mat_ :: iterator end = gray.end (); (; it!= end; ++ it) if(* it)points.push_back(it.pos()); } –
JackTurky
2012-04-25 22:36:03
該部分仍凍結我..而是使用向量,我嘗試cvSeq,好的,但迭代器? – JackTurky 2012-04-25 22:36:39