import cv2 import numpy as np mask = np.zeros([600, 600], np.uint8) mask[200:400, 200:400] = 255 cv2.imshow('mask', mask) cv2.waitKey() cv2.imwrite(mask,mask),加一段代码,将上述代码生成的mask保存在当前文件夹下
时间: 2024-03-16 22:42:37 浏览: 103
你可以加上以下代码来保存生成的 mask:
```python
cv2
import numpy as np
mask = np.zeros([600, 600], np.uint8)
mask[200:400, :400] = 255
cv2.imshow('mask', mask)
cv2.waitKey()
cv2.imwrite('mask.jpg', mask)
```
这段代码中,`cv2.imwrite('mask.jpg', mask)` 将会把生成的 mask 保存在当前文件夹下,并命名为 `mask.jpg`。你也可以根据需要修改文件名和格式。
相关问题
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)
```
注意:这段代码仅供参考,可能还需要根据实际情况进行调整和修改。
优化这段代码import cv2 import imutils import numpy as np img = cv2.imread('D:\pycharm\PycharmProjects\pythonProject\p1\p1.jpg', cv2.IMREAD_COLOR) img = cv2.resize(img, (600, 400)) cv2.imshow('Origin image', img) img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) img_gray = cv2.bilateralFilter(img_gray, 13, 15, 15) img_edged = cv2.Canny(img_gray, 30, 200) cv2.imshow('edged image', img_edged) img_contours = cv2.findContours(img_edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) img_contours = imutils.grab_contours(img_contours) img_contours = sorted(img_contours, key=cv2.contourArea, reverse=True)[:10] screenCnt = None for c in img_contours: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.018 * peri, True) if len(approx) == 4: screenCnt = approx break if screenCnt is None: detected = 0 print("No contour detected") else: detected = 1 if detected == 1: cv2.drawContours(img, [screenCnt], -1, (0, 0, 255), 3) mask = np.zeros(img_gray.shape, np.uint8) new_image = cv2.drawContours(mask, [screenCnt], 0, 255, -1, ) cv2.imshow('mask_image', new_image) new_image = cv2.bitwise_and(img, img, mask=mask) (x, y) = np.where(mask == 255) (topx, topy) = (np.min(x), np.min(y)) (bottomx, bottomy) = (np.max(x), np.max(y)) cropped = img_gray[topx:bottomx + 1, topy:bottomy + 1] cropped = cv2.resize(cropped, (400, 200)) cv2.imshow('Cropped', cropped) cv2.waitKey(0) cv2.destroyAllWindows()
这段代码主要是对一张图片进行边缘检测,并找出图片中的一个矩形区域进行裁剪。下面是对代码的一些优化建议:
1. 将代码拆分成多个函数,增加代码的可读性和可维护性。
2. 将文件路径字符串改为使用原始字符串(raw string),以避免转义字符带来的问题。
3. 考虑使用命令行参数或配置文件来指定输入文件路径和其他参数,以增加代码的灵活性。
4. 添加错误处理机制,例如检查文件是否存在,处理可能出现的异常情况。
5. 使用`with`语句来管理资源,例如`cv2.VideoCapture`和`cv2.imshow`,以确保资源的正确释放。
6. 使用`argparse`模块来解析命令行参数,以方便指定不同的参数和选项。
7. 使用更具描述性的变量名,以增加代码的可读性。
8. 添加注释来解释代码的功能和逻辑。
9. 考虑使用适当的异常处理机制,例如在无法找到矩形区域时抛出异常或返回错误码。
以上是对代码的一些优化建议,你可以根据实际需求进行调整。
阅读全文