Python 如何更改cv2.boundingRect的值
有没有办法更改cv2.boundingRect的值 我想进行调整,以便获得准确的cv2.0轮廓Python 如何更改cv2.boundingRect的值,python,opencv,Python,Opencv,有没有办法更改cv2.boundingRect的值 我想进行调整,以便获得准确的cv2.0轮廓 我不知道你想做什么。但我在Python/OpenCV中看到了两种不同的方法 1) 只要增加w import cv2 # Load image, grayscale, Gaussian blur, Otsu's threshold image = cv2.imread("5.jpg") gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blur = cv
我不知道你想做什么。但我在Python/OpenCV中看到了两种不同的方法 1) 只要增加w
import cv2
# Load image, grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread("5.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Find bounding box
x,y,w,h = cv2.boundingRect(thresh)
w = w + 9
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
cv2.putText(image, "w={},h={}".format(w,h), (x,y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (36,255,12), 2)
cv2.imshow("thresh", thresh)
cv2.imshow("image", image)
cv2.waitKey()
2) 放大你的阈值图像
import cv2
# Load image, grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread("5.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# apply morphology dilate
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel)
# Find bounding box
x,y,w,h = cv2.boundingRect(thresh)
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
cv2.putText(image, "w={},h={}".format(w,h), (x,y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (36,255,12), 2)
cv2.imshow("thresh", thresh)
cv2.imshow("image", image)
cv2.waitKey()
您实际上想如何调整它?此方法使用cv2.rectangle而不使用numpy切片。只需在cv2.boundingRect()之后和cv2.rectangle()之前修改x、y、w、h即可。如果您只是想使绿色矩形的右侧更向右,那么只需增加w。这不是很明显,还是我误解了你想做什么?这解决了我的问题,谢谢
import cv2
# Load image, grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread("5.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# apply morphology dilate
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel)
# Find bounding box
x,y,w,h = cv2.boundingRect(thresh)
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
cv2.putText(image, "w={},h={}".format(w,h), (x,y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (36,255,12), 2)
cv2.imshow("thresh", thresh)
cv2.imshow("image", image)
cv2.waitKey()