我想找到一个图像的轮廓然后画出它的凸包.我正在做的是加载图像,阈值,找到它的轮廓,然后绘制凸包.
gray = cv2.imread(test_paths[i], 0)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
Run Code Online (Sandbox Code Playgroud)
检测到的轮廓数量等于1.如果我尝试绘制轮廓,问题就出现了
cv2.drawContours(cnt_dst, cnt, -1, (255, 0, 0), 3)
plt.imshow(cnt_dst)
Run Code Online (Sandbox Code Playgroud)
如果我将代码更改为以下内容:
cv2.drawContours(cnt_dst, contours, 0, (255, 0, 0), 3)
plt.imshow(cnt_dst)
Run Code Online (Sandbox Code Playgroud)
轮廓不同:
请注意,我得到了相同(好)的结果:
cv2.drawContours(cnt_dst, contours, -1, (255, 0, 0), 3)
Run Code Online (Sandbox Code Playgroud)
有关为什么会发生这种情况的任何想法?
我画了一个极端点周围的轮廓.里面的多边形图我有其他点.如何检查它们是否在轮廓内?
我使用了find contours和boundingrect并在我的项目中显示它.然后我想找到最大的轮廓并显示它.这可能吗?我是OpenCV java lang的新手.
到目前为止我的代码:
@Override
public void onCameraViewStarted(int width, int height) {
mRgba = new Mat(height, width, CvType.CV_8UC4);
mHsv = new Mat(height,width,CvType.CV_8UC3);
hierarchy = new Mat();
mHsvMask = new Mat();
mDilated = new Mat();
mEroded = new Mat();
}
@Override
public void onCameraViewStopped() {
mRgba.release();
mHsv.release();
mHsvMask.release();
mDilated.release();
hierarchy.release();
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mRgba =inputFrame.rgba();
contours = new ArrayList<MatOfPoint>();
hierarchy =new Mat();
mHsv = new Mat();
mHsvMask =new Mat();
Imgproc.cvtColor(mRgba, mHsv, Imgproc.COLOR_RGB2HSV);
Scalar lowerThreshold = new …Run Code Online (Sandbox Code Playgroud) 所以我试图从python中的轮廓绘制convexHull,但是当我打印图像时它没有改变.
roi=mask[y:y+h,x:x+w]
roi = cv2.fastNlMeansDenoisingColored(roi,None,15,15,7,21)
hull = cv2.convexHull(cnt)
cv2.drawContours(roi,[hull],0,(147,0,255),2)
cv2.imshow(str(i),roi)
blank_image[y:y+h,x:x+w] = roi
Run Code Online (Sandbox Code Playgroud)
我有两个二值图像,我试图检测其中白色斑块的轮廓(拼贴右侧的粉红色轮廓是轮廓结果)。
cv2.contourFind() Contour1 工作正常:

但是对于 Contour2,它的表现很奇怪:

这是它的函数调用
#Convert Image to grayscale
img = cv2.imread(file_name)
img2gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
dilated = cv2.dilate(mask, kernel, iterations=2)
image, contours, hierarchy = cv2.findContours(dilated.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for contour in contours:
[x, y, w, h] = cv2.boundingRect(contour)
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 255), 2)
Run Code Online (Sandbox Code Playgroud)
使用这个contours变量,我围绕找到的点绘制矩形。我不明白为什么它适用于 Contour1,但在它们看起来非常相似时却无法用于 Contour2。
我正在尝试将OpenCV与Python结合使用,以便检测来自Raspberry Pi摄像机的实时视频源中的正方形。但是,下面的代码中的cv2.GaussianBlur和cv2.Canny函数导致以下错误:“ TypeError:numpy.ndarray'对象不可调用”。
我似乎无法解决该错误。任何帮助表示赞赏。
import cv2
# load the video
camera = cv2.VideoCapture(0)
# keep looping
while True:
# grab the current frame and initialize the status text
(grabbed, frame) = camera.read()
status = "No Targets"
# check to see if we have reached the end of the
# video
if not grabbed:
break
# convert the frame to grayscale, blur it, and detect edges
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blurred = …Run Code Online (Sandbox Code Playgroud) 我想裁剪图像中最大的对象(字符)。该代码仅在没有行的情况下才有效(如第一个图像所示)。但是我需要忽略这条线并制作第二张图像。仅裁剪最大的对象图像。
import cv2
x1, y1, w1, h1 = (0,0,0,0)
points = 0
# load image
img = cv2.imread('Image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # convert to grayscale
# threshold to get just the signature
retval, thresh_gray = cv2.threshold(gray, thresh=100, maxval=255, type=cv2.THRESH_BINARY)
# find where the signature is and make a cropped region
points = np.argwhere(thresh_gray==0) # find where the black pixels are
points = np.fliplr(points) # store them in x,y coordinates instead of row,col indices
x, y, w, h = cv2.boundingRect(points) …Run Code Online (Sandbox Code Playgroud) 我正在尝试在图像周围绘制轮廓。我可以看到找到了轮廓,但我无法绘制轮廓。轮廓的颜色似乎是两种(黑色和白色)颜色之一。
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
%matplotlib inline
im = io.imread('http://matlabtricks.com/images/post-35/man.png')
plt.imshow(im)
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
plt.figure()
plt.imshow(imgray)
#Contoured image
ret,thresh = cv2.threshold(imgray, 120,255,cv2.THRESH_BINARY)
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
c_img = cv2.drawContours(image, contours, -1, (0, 255, 0), 1)
plt.figure()
plt.imshow(c_img)
Run Code Online (Sandbox Code Playgroud) python opencv image-processing computer-vision opencv-contour
我在图像中有对象集合。在此处检查样本输入图像。
我想找到每个对象的轮廓。我正在按照以下方法使用OpenCV2识别轮廓
gray = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)
edged = cv2.Canny(gray, 50, 100)
dilate= cv2.dilate(edged, None, iterations=1)
erode= cv2.erode(dilate, None, iterations=1)
cnts = cv2.findContours(erode, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
Run Code Online (Sandbox Code Playgroud)
这是上面代码的轮廓输出:请参见输出图像
有没有更好的方法来识别图像中的对象?
我在该网站上找到了以下代码:
import os
import os.path
import cv2
import glob
import imutils
CAPTCHA_IMAGE_FOLDER = "generated_captcha_images"
OUTPUT_FOLDER = "extracted_letter_images"
# Get a list of all the captcha images we need to process
captcha_image_files = glob.glob(os.path.join(CAPTCHA_IMAGE_FOLDER, "*"))
counts = {}
# loop over the image paths
for (i, captcha_image_file) in enumerate(captcha_image_files):
print("[INFO] processing image {}/{}".format(i + 1, len(captcha_image_files)))
# Since the filename contains the captcha text (i.e. "2A2X.png" has the text "2A2X"),
# grab the base filename as the text
filename …Run Code Online (Sandbox Code Playgroud)