-1
A
回答
0
這是你怎麼能像
這是已知的對象/輪廓位置(彩色的東西在那裏)
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 and 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 the 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);
// copy rotated object to original image:
cv::Mat output = input.clone();
rotatedInput.copyTo(output, rotatedMask);
cv::imwrite("rotateObjects_beforeHolefilling.png", output);
// now there are pixel left from the old object position.
cv::Mat holePixelMask = mask & (255-rotatedMask);
// we have to fill those pixel with some kind of background...
cv::Mat legalBackground = (255-mask);
//cv::erode(legalBackground,)
// fill holes. here you could try to find some better background color by 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);
return 0;
}
這是輸入圖像中旋轉對象孔填充前的輸出
這是孔填充
+0
謝謝@Micka。 –
相關問題
- 1. 搜索輪廓內的輪廓/ OpenCV C++
- 2. OpenCV - 如何將像素跟蹤輪廓轉換爲頂點跟蹤輪廓
- 3. 如何在Python OpenCV中去除輪廓內的輪廓?
- 4. OpenCV ||輪廓相似性
- 5. OpenCV輪廓?
- 6. Opencv輪廓
- 7. OpenGL中的輪廓非凸多邊形
- 8. OpenCV中的多邊形輪廓?例如cvApproxPoly()的反義詞?
- 9. OpenCV轉換Canny邊緣到輪廓
- 10. OpenCV:如何找到輪廓/多邊形內的顏色?
- 11. OpenCV-如何統一不同的輪廓,一個封閉輪廓
- 12. 查找相鄰多邊形的輪廓
- 13. 將簡單多邊形的輪廓樣條輪廓線轉換成簡單的多邊形
- 14. OpenCV獲取最近的輪廓點
- 15. OpenCV中的輪廓陣列
- 16. opencv中的重複輪廓
- 17. OpenCV的Python中:找到輪廓/邊緣/矩形圖像
- 18. 提取外部輪廓或圖像的輪廓在Python
- 19. 輪廓形狀
- 20. 輪廓opencv:如何消除二進制圖像中的小輪廓
- 21. OpenCV輪廓時刻?
- 22. Opencv輪廓錯誤
- 23. (opencv)合併輪廓
- 24. OpenCV輪廓方向
- 25. OpenCV分割輪廓
- 26. 在OpenCV中尋找輪廓?
- 27. 在OpenCV中使用輪廓?
- 28. 在opencv中填充輪廓
- 29. 在OpenCv中擴展輪廓
- 30. OpenCV:從輪廓樹轉換爲輪廓 - cvContourFromContourTree()
後,你在輪廓的新方向感興趣,或者你要旋轉的那個輪廓的「像素」(例如:內旋轉一個單一的對象圖像和其他應保持不變)? – Micka
是的,我想旋轉輪廓的「像素」。舉例來說:「在圖像中旋轉一個單獨的對象,其餘的應該保持不變」 –
旋轉之前遮擋的像素應該做什麼?我的建議:從輪廓中創建一個填充的蒙版(例如fillpoly函數)。旋轉整個圖像並旋轉蒙版。從旋轉後的圖像中將旋轉後的圖像設置爲原始圖像的像素。要查找旋轉前遮擋的像素,請比較旋轉和未旋轉的蒙版。 – Micka