1
我需要爲白色像素雲找到最小區域矩形(cv2.minAreaRect())。找到在opencv中設置的白色像素的邊界框(python)
我在圖像中有多個白色物體,我想在它們周圍繪製矩形。
我發現在C++中的解決方案:
cv::cvtColor(img, gray, CV_BGR2GRAY);
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());
}
cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));
這是我嘗試在Python:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5 ,5), 0)
retval, thresh = cv2.threshold(gray, 210, 255, cv2.THRESH_BINARY)
whitep = []
for y, row in enumerate(thresh):
for x, px in enumerate(row):
if px == 255:
whitep.append((y, x))
box = cv2.minAreaRect(whitep)
但它並不作品:
box = cv2.minAreaRect(whitep)
TypeError: <unknown> is not a numpy array
我該怎麼辦? 謝謝
嘗試:'盒= cv2.minAreaRect(numpy.array(白皮書))' –
我已經嘗試:錯誤:(-210)的基質不能被轉換爲點序列因爲不合適的元素類型 – frx08
答案似乎在這裏:http://stackoverflow.com/questions/8369547/checking-contour-area-in-opencv-using-python;嘗試:'box = cv2.minAreaRect(numpy.array([whitep],dtype = numpy.int32))' –