如何解决计算共定位对象
我正在尝试编写代码,以自动计算图中的圆形对象。对我来说,我想计算包含(例如)蓝色,红色和蓝色+红色染色的圆圈(图像中的核)的数量。到目前为止,我已经获得了一个计算所有蓝色和所有红色圆圈的代码。我很难编写/修改代码以获取蓝色和红色的圆圈(共定位)。我已经在Google Colab上撰写了这篇文章。这是输出一个.csv文件的代码,该文件在用户上传的图片中的文件名计数为红色,绿色和蓝色圆圈。
from google.colab import files
from PIL import Image
import matplotlib.pyplot as plt
import cv2
import numpy as np
import pandas as pd
object_count = dict()
image_list = []
#### Object Counter Class
class ObjectCounter:
def count(self):
genotype = input('Genotype: ')
nImages = input('Number of images to quantify: ')
a = 0
while a < int(nImages):
uploaded = files.upload()
fileName = input('File name: ')
image_list.append(fileName)
low_intensity = int(input('Lower intensity: '))
img = cv2.imread(fileName)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.show()
r,g,b = cv2.split(img)
colors = ['Red','Green','Blue']
channels = [r,b]
x = 0
for i in channels:
seed_pt = (20,20)
fill_color = 0
mask = np.zeros_like(i)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
for th in range(low_intensity,255):
prev_mask = mask.copy()
mask = cv2.threshold(i,th,255,cv2.THRESH_BINARY)[1]
mask = cv2.floodFill(mask,None,seed_pt,fill_color)[1]
mask = cv2.bitwise_or(mask,prev_mask)
mask = cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernel)
plt.imshow(mask)
plt.show()
n_centers = cv2.connectedComponents(mask)[0] - 1
print('There are ' + str(n_centers) + ' objects in the image.')
if colors[x] in object_count:
object_count[colors[x]].append(n_centers)
else:
object_count[colors[x]] = [n_centers]
x = x + 1
df1 = pd.DataFrame(image_list,columns=['File Name'])
df2 = pd.DataFrame.from_dict(object_count)
a = a + 1
# Save the final counting data
data = pd.concat([df1,df2],axis=1)
print(data)
output_fileName = input('Output .csv File Name: ')
data.to_csv(output_fileName)
files.download(output_fileName)
## Main Function
countObj = ObjectCounter()
countObj.count()
这是一个示例图像,可用于测试代码。点击here。
有人可以帮助我更新/改进此代码,以同时获得蓝色和红色的圆圈数吗?
预先感谢
Kasun
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。