cv2.imshow('result', img)
时间: 2023-10-23 12:11:46 浏览: 130
`cv2.imshow()`函数是OpenCV中用于显示图像的函数。该函数的语法如下:
```python
cv2.imshow(winname, mat)
```
该函数的参数解释如下:
- `winname`:窗口的名称,字符串类型。
- `mat`:要显示的图像,可以是NumPy数组、PIL图像或其他类型的图像。
以下是一个示例代码,演示了如何使用`cv2.imshow()`函数显示一张图像:
```python
import cv2
# 读取图像
img = cv2.imread('image.jpg')
# 显示图像
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在这个示例中,程序首先读取一张图像,然后使用`cv2.imshow()`函数显示图像,参数`'image'`表示窗口的名称。程序使用`cv2.waitKey(0)`等待用户按下任意键,然后使用`cv2.destroyAllWindows()`销毁所有窗口。你可以根据需要调整窗口的名称和图像的路径、名称等参数。
相关问题
import cv2 import numpy as np def main(): # 1.导入图片 img_src = cv2.imread("cc.jpg") # 2.灰度化,二值化 img_gray = cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY) ret, img_bin = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY) # 3.连通域分析 img_bin, contours, hierarchy = cv2.findContours(img_bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # 4.获取最小外接矩形并打印 min_rect = cv2.minAreaRect(contours[0]) print("返回值min_rect:\n", min_rect) rect_points = cv2.boxPoints(min_rect) print("返回值rect_points:\n", rect_points) rect_points = np.int0(rect_points) # 5.绘制最小外接矩形 img_result = img_src.copy() cv2.drawContours(img_result, [rect_points], 0, (255, 255, 255), 2) # 6.显示结果图片 cv2.imshow("img_src", img_src) cv2.imshow("img_result", img_result) cv2.waitKey() cv2.destroyAllWindows() if __name__ == '__main__': main()怎么把图像摆正
要将图像摆正,您可以使用cv2.warpAffine()函数。该函数使用变换矩阵将图像旋转、平移、缩放等转换。以下是一个示例,可以将最小外接矩形旋转到水平方向:
```python
# 计算旋转角度
angle = min_rect[2]
if angle < -45:
angle += 90
# 计算旋转中心
center = tuple(np.array(img_src.shape[1::-1]) / 2)
# 生成变换矩阵
M = cv2.getRotationMatrix2D(center, angle, 1)
# 应用变换矩阵
img_result_rotated = cv2.warpAffine(img_result, M, img_src.shape[1::-1], flags=cv2.INTER_CUBIC)
```
这将生成一个旋转后的图像img_result_rotated。您可以将其保存为图像文件并使用cv2.imshow()函数显示。
import cv2 import numpy as np img = cv2. imread(' blood. BMP',-1) cv2. imshow("source".img) dst = cv2.blur(img,(3,3)) ret, thresh = cv2.threshold(dst,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) cv2. imshow("thresh".thresh) kernel = np.ones((4,4),np.uint8) opening = cv2.morphologyEx (thresh,cv2. MORPH_OPEN,kernel,iterations=2) kernel1 = np.ones((3, 3).np.uint8) close = cv2.morphologyEx(openning,cv2.MORPH_CLOSE,kernel1) cv2. imshow("opening",close) temp = close.copy () h, w = close. shape[:2] mask= np.zeros((h+2,w+2).np.uint8) cv2.floodFill(temp,mask,(230,145),255) temp_inv = cv2.bitwise_not(temp) result = close|temp_inv cv2. imshow("result",result) contours, hirearchy = cv2. findContours(result,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) count = 0 area = 0 for i in contours: if cv2.contourArea(i)>73: area+=cv2.contourArea(i) count+=1 count1 = 0 for i in contours: if cv2. contourArea(i)>73: count1+=1 if cv2. contourArea(i)>(1.25*area/count): count1+=1 if cv2. contourArea(i)>(2.1*area/count): count1+=1 print("细胞有%d个"%count1) cv2. waitKey(0)
这段代码中有一些语法错误和拼写错误。以下是修正后的代码:
```
import cv2
import numpy as np
img = cv2.imread('blood.BMP', -1)
cv2.imshow("source", img)
dst = cv2.blur(img, (3, 3))
ret, thresh = cv2.threshold(dst, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
cv2.imshow("thresh", thresh)
kernel = np.ones((4, 4), np.uint8)
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)
kernel1 = np.ones((3, 3), np.uint8)
close = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel1)
cv2.imshow("opening", close)
temp = close.copy()
h, w = close.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)
cv2.floodFill(temp, mask, (230, 145), 255)
temp_inv = cv2.bitwise_not(temp)
result = close | temp_inv
cv2.imshow("result", result)
contours, hierarchy = cv2.findContours(result, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
count = 0
area = 0
for i in contours:
if cv2.contourArea(i) > 73:
area += cv2.contourArea(i)
count += 1
count1 = 0
for i in contours:
if cv2.contourArea(i) > 73:
count1 += 1
if cv2.contourArea(i) > (1.25 * area / count):
count1 += 1
if cv2.contourArea(i) > (2.1 * area / count):
count1 += 1
print("细胞有%d个" % count1)
cv2.waitKey(0)
```
注意:这段代码仅供参考,可能还需要根据实际情况进行调整和修改。
阅读全文