我正在用Python学习OpenCV,并且我想学习如何计算图像中的对象/元素。
我写了一个用于计数的代码,但是得到了错误的结果。图片中有12个元素,而我得到40个元素,但是有些元素没有计算在内。
我不知道我在做什么错。
这是我的代码:
import cv2
img = cv2.imread('slika.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print('There are 12 elements on this image')
#cv2.imshow('img', gray)
#cv2.waitKey(0)
ret,thresh = cv2.threshold(gray,127,255,1)
contours,h = cv2.findContours(thresh,1,1)
print('Number of elements found:', len(contours))
for cnt in contours:
cv2.drawContours(img,[cnt],0,(0,0,255),2)
cv2.imshow('img', img)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)
这是默认图像,包含12个元素:
结果如下:
您会看到无法识别粉色和两个黄色元素,但这是绿色元素的问题。
我在做什么错,以及如何解决?
我想删除图像的黑色和红色圆圈,而又不影响图像内部的红色方块(因为红色圆圈和红色方块的像素值相同)。
我尝试使用cv2.HoughCircles检测红色圆圈并将其转换为黑色,但红色圆圈的某些部分保持不变,如图所示。
这是我用于此的代码。
import numpy as np
import cv2
image = cv2.imread("13-14.png")
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.3, 145)
if circles is not None:
circles = np.round(circles[0, :]).astype("int")
for (x, y, r) in circles:
cv2.circle(output, (x, y), r, (0, 0 , 0), 4)
cv2.imshow("output", np.hstack([image, output]))
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)
有什么建议么?提前致谢。
编辑1
我正在寻找的样本输出就是这种图像(彩色或灰度)。
OpenCV 中是否有一个开箱即用的improfile函数来沿着一条线从图像中提取强度分布,类似于MATLAB 中的函数?
我有一个巨大的图像数据集,如下所示:
我想改变这些的颜色。所有白色应保持白色,所有紫色应变为白色,其他所有内容应变为黑色。所需的输出如下所示:

我已经在下面编写了代码,它正在执行我想要的操作,但是需要很长时间才能浏览我拥有的大量图片。还有另一种更快的方法吗?
path = r"C:path"
for f in os.listdir(path):
f_name = (os.path.join(path,f))
if f_name.endswith(".png"):
im = Image.open(f_name)
fn, fext = os.path.splitext(f_name)
print (fn)
im =im.convert("RGBA")
for x in range(im.size[0]):
for y in range(im.size[1]):
if im.getpixel((x, y)) == (255, 255, 255, 255):
im.putpixel((x, y),(255, 255, 255,255))
elif im.getpixel((x, y)) == (128, 64, 128, 255):
im.putpixel((x, y),(255, 255, 255,255))
else:
im.putpixel((x, y),(0, 0, 0,255))
im.show()
Run Code Online (Sandbox Code Playgroud) 我正在尝试在摄像机录制的视频中检测人脸。当我使用网络摄像头视频时,它工作正常。但是,对于摄像机录制的视频,视频会旋转 -90 度。请建议我,如何获得用于人脸检测的实际视频输出?
import cv2
import sys
cascPath = sys.argv[1]
faceCascade = cv2.CascadeClassifier('C:/Users/HP/Anaconda2/pkgs/opencv-3.2.0-np112py27_204/Library/etc/haarcascades/haarcascade_frontalface_default.xml')
#video_capture = cv2.videoCapture(0)
video_capture = cv2.VideoCapture('C:/Users/HP/sample1.mp4')
w=int(video_capture.get(3))
h=int(video_capture.get(4))
#output = cv2.VideoWriter('output_1.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 60,frameSize = (w,h))
while True:
ret, frame = video_capture.read()
frame = rotateImage(frame,90)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, 1.3, 5)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
#cv2.imshow('face',i)
#output.write(frame)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release() …Run Code Online (Sandbox Code Playgroud) I have a cell array in MATLAB in the following format:
aa = {[1 2],[2 3],[1 2 3 4 5 6],[5],[1]}
Run Code Online (Sandbox Code Playgroud)
Is it possible to create a diagram in this style (i.e. filling in a block colour for each time a number shows up)?
我正在尝试更改颜色空间L*中图像的值L*a*b*。但最终的图像并不是我所期望的。我应该如何使用这些值更改图像的亮度L*a*b*?
我的代码:
imd = np.asarray(ibuffer).copy()
imgb = cv2.cvtColor(imd, cv2.COLOR_BGR2Lab)
value = 255 * (int(bvalue)/100)
imgb[:,:,0] += int(value)
imgb = cv2.cvtColor(imgb,cv2.COLOR_LAB2BGR)
photo = Image.fromarray(imgb)
photo = resize(photo)
photo = ImageTk.PhotoImage(photo)
canvas.photo = photo
canvas.create_image(0,0,anchor="nw",image = photo)
Run Code Online (Sandbox Code Playgroud)
原图:

编辑后的图像:

我有这个图像:
并且,想像这样剪掉透明背景:
使用 PIL 将会是:
from PIL import Image
im = Image.open("image")
im.getbbox()
im2 = im.crop(im.getbbox())
im2.save("result")
Run Code Online (Sandbox Code Playgroud)
但是,如何使用 OpenCV 变体来做到这一点?
for cnt in contours:
peri=cv2.arcLength(contours[i],True)
aprx=cv2.approxPolyDP(contours[i],0.04*peri,True)
if len (aprx) >5:
c=c+1
M = cv2.moments(cnt)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
Areaa = cv2.contourArea(cnt)
print (image[cX,cY])
if len (aprx)==4:
squareM=cv2.moments(cnt)
SX= int(squareM["m10"] / squareM["m00"])
SY = int(squareM["m01"] / squareM["m00"])
print("hehehe",SX,SY)
i=i+1
cv2.imshow('img',image)
cv2.imshow('thresh',thresh)
Run Code Online (Sandbox Code Playgroud) 我想查看热图像、RGB 图像、灰度图像和二值图像的通道数。
所以我写了这个程序:
import cv2
import numpy
img = cv2.imread("B2DBy.jpg")
print('No of Channel is: ' + str(img.ndim))
cv2.imshow("Channel", img)
cv2.waitKey()
Run Code Online (Sandbox Code Playgroud)
但它为所有类型的图像提供了相同的三通道结果?我已经阅读了这个问题,但它给出了一个错误:
img = cv2.imread("B2DBy.jpg", CV_LOAD_IMAGE_UNCHANGED)
NameError: name 'CV_LOAD_IMAGE_UNCHANGED' is not defined
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:这是查看频道数量的正确方法吗?或者,不知何故,我一直输入三通道图像,因此它给出了三通道输出?
我的输入:
我想为附加图像提取 2 个蒙版中的水平线和垂直线。
我尝试过形态学操作来做到这一点,
horizontalStructure = cv.getStructuringElement(cv.MORPH_RECT, (horizontal_size, 1))
verticalStructure = cv.getStructuringElement(cv.MORPH_RECT, (1, verticalsize))
Run Code Online (Sandbox Code Playgroud)
但问题是,它将线检测为矩形,然后以2条线代表矩形2条边的形式绘制它。
有什么想法可以解决这个问题吗?
横向结果:
垂直结果:
编辑:那是我的另一张图片: