计算共定位对象

问题描述

我正在尝试编写代码,以自动计算图中的圆形对象。对我来说,我想计算包含(例如)蓝色,红色和蓝色+红色染色的圆圈(图像中的核)的数量。到目前为止,我已经获得了一个计算所有蓝色和所有红色圆圈的代码。我很难编写/修改代码以获取蓝色和红色的圆圈(共定位)。我已经在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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...