c++ - How to rotate pixels of contour or approximate polygon of contour in OpenCV? -
c++ - How to rotate pixels of contour or approximate polygon of contour in OpenCV? -
after finding contours in image, consider have contours pixels , approximate polygon of it.
i want rotate contours pixels or approximate polygon of contour given angle. possible in opencv?
this how can rotate object within image
this input image known object/contour position (the colored thing there)
int main() { cv::mat input = cv::imread("rotateobjects_input.png"); std::vector<cv::point> mycontour; mycontour.push_back(cv::point(100,100)); mycontour.push_back(cv::point(150,100)); mycontour.push_back(cv::point(150,300)); mycontour.push_back(cv::point(100,300)); cv::point2f cog(0,0); for(unsigned int i=0; i<mycontour.size(); ++i) { cog = cog + cv::point2f(mycontour[i].x, mycontour[i].y); } cog = 1.0f/(float)mycontour.size()*cog; std::cout << "center of gravity: " << cog << std::endl; // create , draw mask cv::mat mask = cv::mat::zeros(input.size(), cv_8uc1); cv::fillconvexpoly(mask,mycontour,255); // create rotation mat float angledeg = 45; cv::mat transformation = cv::getrotationmatrix2d(cog,angledeg,1); std::cout << transformation << std::endl; // rotated mask holds object position after rotation cv::mat rotatedmask; cv::warpaffine(mask,rotatedmask,transformation,input.size()); cv::mat rotatedinput; cv::warpaffine(input,rotatedinput,transformation, input.size()); cv::imshow("input",input); cv::imshow("rotated input",rotatedinput); cv::imshow("rotated mask",rotatedmask); // re-create rotated object original image: cv::mat output = input.clone(); rotatedinput.copyto(output, rotatedmask); cv::imwrite("rotateobjects_beforeholefilling.png", output); // there pixel left old object position. cv::mat holepixelmask = mask & (255-rotatedmask); // have fill pixel kind of background... cv::mat legalbackground = (255-mask); //cv::erode(legalbackground,) // fill holes. here seek find improve background color averaging in neighborhood or sth. cv::vec3b lastlegalpixel(0,0,0); for(unsigned int j=0; j<mask.rows; ++j) for(unsigned int i=0; i<mask.cols; ++i) { if(holepixelmask.at<unsigned char>(j,i)) { output.at<cv::vec3b>(j,i) = lastlegalpixel; } else { if(legalbackground.at<unsigned char>(j,i)) lastlegalpixel = input.at<cv::vec3b>(j,i); } } cv::imshow("holes before filling", holepixelmask); cv::imshow("legal background", legalbackground); cv::imshow("result", output); cv::waitkey(-1); homecoming 0; }
this output before hole filling
and after hole filling
c++ opencv
Comments
Post a Comment