예제 #1
0
def match_image_template(image, template_dir, thresh):
    """
    A function to match the templates with the image.
    Input: 
    image: Image from which needs to find patterns
    template_dir: A directory containing (cropped) template images
    thresh: Score Threshold Value. 

    Output: matched patterns in pandas dataframe
    BBox, Score, templateName

    """

    list_ = []

    for i in os.listdir(template_dir):
        template = cv2.imread(template_dir + i, 0)
        list_.append((str(i), template))

    match = matchTemplates(list_,
                           image,
                           score_threshold=thresh / 100,
                           method=cv2.TM_CCOEFF_NORMED,
                           maxOverlap=0.30)
    print("Found {} Ellipses".format(len(match.index)))
    print(match)
    return match
예제 #2
0
def locate_particles(frame, template, match_features=features_match):
    hits = matchTemplates(
        template.create(),
        frame,
        N_object=match_features.get('N_obj'),
        score_threshold=match_features.get('threshold'),
        method=cv2.TM_CCOEFF_NORMED,
        maxOverlap=match_features.get('max_overlap')).sort_index()
    hits.index = range(
        len(hits)
    )  #WARNING: this tris to solve the problem with matchTemplate, as it produces a dataframe with missing indices
    if template.find_centres() is None:
        return hits
    else:
        centres = template.find_centres()
        hits1 = hits['BBox']
        bbox = []
        for i in range(len(hits)):
            x = hits1[i][0] + centres.get(hits.TemplateName[i])[0]
            y = hits1[i][1] + centres.get(hits.TemplateName[i])[1]
            wx = hits1[i][2]
            wy = hits1[i][3]
            bbox.append((x, y, wx, wy))
        hits.BBox = bbox
        return hits
예제 #3
0
    def check_coins(self):
        ind = 0
        max_index = len(self.coin_pos)
        while ind < max_index:
            coin1_pos = self.coin_pos[ind]
            coin2_pos = self.coin_pos[ind + 1]

            mouse_click(coin1_pos[0] + coin1_pos[2] / 2,
                        coin1_pos[1] + coin1_pos[3] / 2,
                        wait=0.1)
            mouse_click(coin2_pos[0] + coin2_pos[2] / 2,
                        coin2_pos[1] + coin2_pos[3] / 2,
                        wait=0.3)
            screen = cv2.imread(screen_grab())
            matches = matchTemplates(self.coin_images,
                                     screen,
                                     N_object=2,
                                     score_threshold=.7,
                                     maxOverlap=.25,
                                     searchBox=None)

            coin1 = (matches["TemplateName"][0], matches["BBox"][0])
            coin2 = (matches["TemplateName"][1], matches["BBox"][1])

            if coin1[0] == coin2[0]:
                self.coin_items.pop(coin1[0])
            else:
                self.coin_items[coin1[0]].append(coin1[1])
                self.coin_items[coin2[0]].append(coin2[1])

            ind += 2
예제 #4
0
def locate_templates(frame, list_template, match_features=features_match):
    return matchTemplates(
        list_template,
        frame,
        N_object=match_features.get('N_obj'),
        score_threshold=match_features.get('threshold'),
        method=cv2.TM_CCOEFF_NORMED,
        maxOverlap=match_features.get('max_overlap')).sort_index()
예제 #5
0
 def get_coin_fields(self):
     screen = cv2.imread(screen_grab())
     matches = matchTemplates(
         [("card", cv2.imread("rc_items/coinflip_back.png"))],
         screen,
         N_object=float("inf"),
         score_threshold=0.5,
         #maxOverlap=0.25,
         searchBox=None)
     for i in range(len(matches['BBox'])):
         self.coin_pos.append(matches['BBox'][i])
예제 #6
0
def find_image(image_path, root_image_path):
    matches = matchTemplates(
        [("img", cv2.imread(image_path))],
        cv2.imread(root_image_path),
        N_object=10,
        score_threshold=0.9,
        # maxOverlap=0.25,
        searchBox=None)
    if len(matches["BBox"]) == 0:
        return None, None
    else:
        box = matches["BBox"][0]
        return box[0], box[1]
def TemplateMmatching(tarImg, hostImg, thres):

    target = cv2.cvtColor(tarImg, cv2.COLOR_BGR2GRAY)
    listTarget = [('target', target)]
    host = cv2.cvtColor(hostImg, cv2.COLOR_BGR2GRAY)
    for i, angle in enumerate([90, 180]):
        rotated = np.rot90(target, k=i +
                           1)  # NB: rotate not good here, turns into float!
        listTarget.append((str('target'), rotated))
    hits = matchTemplates(listTarget,
                          host,
                          score_threshold=thres,
                          method=cv2.TM_CCOEFF_NORMED,
                          maxOverlap=0)
    return hits
예제 #8
0
def find_point_symbol(list_template):
    for template in list_template:
        listTemplate = [('template', template)]
        for i, angle in enumerate([90, 180]):
            rotated = np.rot90(
                template,
                k=i + 1)  # NB: np.rotate not good here, turns into float!
            listTemplate.append((str(angle), rotated))
        Hits = matchTemplates(listTemplate,
                              image_gray,
                              N_object=1,
                              score_threshold=0.6,
                              method=cv2.TM_CCOEFF_NORMED,
                              maxOverlap=0.3)
        point = Hits.values.tolist()[0][1]
        point_plot = (int(point[0] + (point[2] / 2)),
                      int(point[1] + (point[3] / 2)))

        cv2.circle(image, point_plot, 2, (0, 255, 0), -1)
예제 #9
0
def find_point_symbol(image, list_template):
    font = cv2.FONT_HERSHEY_SIMPLEX
    image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    point_symbol = []
    for template in list_template:
        # template = cv2.resize(template, (80, 80))
        # template = cv2.flip(template, 1)
        # template = template[10:90, 10:90]
        # cv2.imshow("thin9",template)
        # cv2.waitKey(0)
        listTemplate = [('template', template)]
        for i, angle in enumerate([90, 180]):
            rotated = np.rot90(
                template,
                k=i + 1)  # NB: np.rotate not good here, turns into float!
            listTemplate.append((str(angle), rotated))
        Hits = matchTemplates(listTemplate,
                              image_gray,
                              N_object=1,
                              score_threshold=0.4,
                              method=cv2.TM_CCOEFF_NORMED,
                              maxOverlap=1)
        point = Hits.values.tolist()[0][1]
        # print(Hits)
        if float((Hits.values.tolist()[0][2])) >= 0.6:
            point_plot = (int(point[0] + (point[2] / 2)),
                          int(point[1] + (point[3] / 2)))
            point_symbol.append(point_plot)

    point_symbol = Sam_OOk_isas(point_symbol, thres=5)

    for point in point_symbol:
        cv2.putText(image, (str(point[0]) + "," + str(point[1])), point, font,
                    0.5, (255, 0, 0), 2)
        cv2.circle(image, point, 2, (0, 255, 0), -1)
    # print(point_symbol)
    return point_symbol
import matplotlib.pyplot as plt
import numpy as np

fileID = r"C:\Users\MattiaV\Desktop\università\Interships\DynamicsOfGranularShapes\ParticleTracking\Images\First frames\f1.png"
image = io.imread(fileID) 
temp_draft = image[300:500,500:670]
temp0 = temp_draft[41:113,48:123]
image2 = io.imread(r"C:\Users\MattiaV\Desktop\università\Interships\DynamicsOfGranularShapes\ParticleTracking\Images\First frames\f2.png")

listTemplate = []

# Initialise figure
f, axarr = plt.subplots(1,4)


for i in np.arange(0, 360, 90):
    #rotated1 = t.rotate(temp0, i, preserve_range=True) 
    #rotated = np.rint(rotated1).astype(int)           # transforms array of float into array of int
    rotated = np.rot90(temp0, k=int(i/90)) # NB: np.rotate not good here, turns into float!
    listTemplate.append( (str(i%120), rotated ) )
    axarr[int(i/90)].imshow(rotated)
    # We could also do some flipping with np.fliplr, flipud

Hits = matchTemplates(listTemplate, image2, N_object=50, score_threshold=0.55, method=cv2.TM_CCOEFF_NORMED, maxOverlap=0.22).sort_index()

Overlay = drawBoxesOnRGB(image, Hits, boxThickness=5)
plt.figure(figsize = (30,30))
plt.axis("off")
plt.imshow(Overlay)

예제 #11
0
for i, angle in enumerate([2, 5]):
    rotated2 = ndimage.rotate(
        Template1, 2,
        reshape=False)  # NB: np.rotate not good here, turns into float!
    listTemplates.append((str(angle), rotated2))
    axarr[i + 1].imshow(rotated2, cmap="gray")

    # We could also do some flipping with np.fliplr, flipud

# In[5]:

Hits = matchTemplates(
    listTemplates,
    SampleImg,
    method=cv2.TM_CCOEFF_NORMED,
    score_threshold=0.89,
    maxOverlap=0,
)

print("Found {} hits".format(len(Hits.index)))
Hits
#Is it becuase the images are not floated?

# In[6]:

#Show Image with Template Lables

get_ipython().run_line_magic('matplotlib', 'notebook')
Overlay = drawBoxesOnRGB(ColorImg, Hits, showLabel=True)
plt.imshow(Overlay)
예제 #12
0
re = cv2.imread(r'C:\Users\aminb\Desktop\FIBO\Image\symbol_module\symbol2.png',
                0)
rr = cv2.imread(r'C:\Users\aminb\Desktop\FIBO\Image\symbol_module\symbol3.png',
                0)
plt.show()
# 1st format the template into a list of tuple (label, templateImage)
listTemplate = [('small', template)]

# Initialise figure
# _, axarr = plt.subplots(1,3)
# axarr[0].imshow(smallCoin, cmap="gray")

for i, angle in enumerate([90, 180]):
    rotated = np.rot90(template, k=i +
                       1)  # NB: np.rotate not good here, turns into float!
    listTemplate.append((str(angle), rotated))

Hits = matchTemplates(listTemplate,
                      image_gray,
                      N_object=1,
                      score_threshold=0.6,
                      method=cv2.TM_CCOEFF_NORMED,
                      maxOverlap=0.3)

print(Hits)
point = Hits.values.tolist()[0][1]
point_plot = (int(point[0] + (point[2] / 2)), int(point[1] + (point[3] / 2)))

cv2.circle(image, point_plot, 2, (0, 255, 0), -1)
cv2.imshow("Ki", image)
cv2.waitKey(0)
        try:
            image = io.imread(file, 0)  ########## reading image
            image = image[0:500, 165:500]
            name1 = file.split("/")[-1]
            name1 = name1.split(".")[0]

            im = image
            noise = np.empty_like(im, dtype="int8")
            level = 10
            cv2.randn(noise, (0), (level))  # Matrix element are 0 in average

            imageNoise = cv2.add(im, noise, dtype=cv2.CV_8U)

            Hits_Noise = matchTemplates(listTemplate,
                                        imageNoise,
                                        N_object=90,
                                        score_threshold=0.001,
                                        method=cv2.TM_CCOEFF_NORMED,
                                        maxOverlap=.08)

            H = Hits_Noise
            df = Hits_Noise.reset_index()

            w = df["BBox"].str[2]
            h = df["BBox"].str[3]
            df["width"] = round(w * 2.54 / 96, 1)
            df["hight"] = round(h * 2.54 / 96, 1)

            f1 = df.TemplateName.unique()

            br1 = df['TemplateName'].value_counts().reset_index()
예제 #14
0
        # resize the marker according to the scale, and keep track
        # of the ratio of the resizing
        new_copy = paper_gray.copy()

        resized = imutils.resize(omr_marker,
                                 width=int(omr_marker.shape[1] * scale))
        r = omr_marker.shape[1] / float(resized.shape[1])

        if resized.shape[0] > height or resized.shape[1] > width:
            break

        listTemplate = [('marker', resized)]
        Hits = matchTemplates(
            listTemplate,
            new_copy,
            N_object=4,
            score_threshold=0.4,
            maxOverlap=0,
            method=cv2.TM_CCOEFF_NORMED)  #,searchBox=(76,781,1856,353)

        Overlay = paper.copy()

        for index in Hits.index:
            x1, y1, w, h = Hits.BBox[index]
            Overlay = drawBoxesOnRGB(paper, Hits)
            Sum_scores = Sum_scores + Hits.Score[index]

        #print("Sum_scores : ",Sum_scores)

        #cv2.imshow('template_matched_paper',cv2.resize(Overlay,(620,620)))
        #cv2.waitKey(0)
예제 #15
0
import matplotlib.pyplot as plt
import numpy as np

image = "Testcopy.png"
plt.figure(figsize=(10, 10))
plt.axis("off")
plt.imshow(image, cmap="gray")

temp0 = image[784:784 + 400, 946:946 + 414]  # with well 49
plt.axis("off")
plt.imshow(temp0, cmap="gray")

listTemplate = [('temp0', temp0)]
Hits = matchTemplates(listTemplate,
                      image,
                      N_object=4,
                      score_threshold=0.3,
                      method=cv2.TM_CCOEFF_NORMED,
                      maxOverlap=0.3)
print(Hits)

# Generate gaussian distributed noise, the noise intensity is set by the level variable
noise = np.empty_like(image, dtype="int8")
level = 50
cv2.randn(noise, (0), (level))  # Matrix element are 0 in average

plt.figure(figsize=(10, 10))
plt.axis("off")
plt.imshow(noise, cmap="gray")

imageNoise = cv2.add(image, noise, dtype=cv2.CV_8U)
예제 #16
0
        im = image
        #Hits = matchTemplates(listTemplate, im,N_object=34, score_threshold=0.5, method=cv2.TM_CCOEFF_NORMED, maxOverlap=0.25)
        #print(Hits)

        # Generate gaussian distributed noise, the noise intensity is set by the level variable
        noise = np.empty_like(im, dtype="int8")
        level = 10
        cv2.randn(noise, (0), (level))  # Matrix element are 0 in average

        imageNoise = cv2.add(im, noise, dtype=cv2.CV_8U)

        Hits_Noise = matchTemplates(
            listTemplate,
            imageNoise,
            score_threshold=0.75,
            method=cv2.TM_CCOEFF_NORMED,
            maxOverlap=0.2,
            searchBox=None)  ##########without using N_object

        #Hits_Noise = matchTemplates(listTemplate, imageNoise,N_object=20,score_threshold=0.001, method=cv2.TM_CCOEFF_NORMED, maxOverlap=.08,searchBox=None)

        H = Hits_Noise
        df = Hits_Noise.reset_index()

        w = df["BBox"].str[2]
        h = df["BBox"].str[3]
        df["width"] = round(w * 2.54 / 96, 1)
        df["hight"] = round(h * 2.54 / 96, 1)

        #score=max(df.Score)
예제 #17
0
        image_nored = removeRedRegionFromImage(BGR_cropimg, mask_closedred)

        cv2.imshow('No red white region', image_nored)
        cv2.waitKey(1)

        #In[4]: APPLY TEMPLATE MATCHING WITHOUT RED REGION

        listTemplate_inwhite = [('A', BGR_featureimg_kngp),
                                ('B', BGR_featureimg_kngp_2),
                                ('G', BGR_featureimg_kngp_3),
                                ('D', BGR_featureimg_kngp_4)
                                ]  #,('E', BGR_featureimg_kngp_5)]
        Hits = matchTemplates(listTemplate_inwhite,
                              image_nored,
                              score_threshold=0.55,
                              method=cv2.TM_CCOEFF_NORMED,
                              maxOverlap=0)
        Overlay_inwhite = drawBoxesOnRGB(image_nored, Hits, showLabel=True)

        total_inwhite = len(Hits)

        listTemplate_blueinwhite = [('*A', BGR_featureimg_kngn),
                                    ('*B', BGR_featureimg_kngn_2),
                                    ('*C', BGR_featureimg_kngn_3)]
        Hits_blueinwhite = matchTemplates(listTemplate_blueinwhite,
                                          image_nored,
                                          score_threshold=0.75,
                                          method=cv2.TM_CCOEFF_NORMED,
                                          maxOverlap=0)
        Overlay_blueinwhite = drawBoxesOnRGB(image_nored,
예제 #18
0
from skimage.data import coins
import matplotlib.pyplot as plt

image = coins()
plt.imshow(image, cmap="gray")

smallCoin = coins()[37:37 + 38, 80:80 + 41]
plt.imshow(smallCoin, cmap="gray")

# 1st format the template into a list of tuple (label, templateImage)
listTemplate = [('small', smallCoin)]

# Then call the function matchTemplates (here a single template)
Hits = matchTemplates(listTemplate,
                      image,
                      score_threshold=0.5,
                      method=cv2.TM_CCOEFF_NORMED,
                      maxOverlap=0)

print("Found {} hits".format(len(Hits.index)))

Overlay = drawBoxesOnRGB(image, Hits, showLabel=True)
plt.imshow(Overlay)

Hits = matchTemplates(listTemplate,
                      image,
                      score_threshold=0.4,
                      method=cv2.TM_CCOEFF_NORMED,
                      maxOverlap=0)
Overlay = drawBoxesOnRGB(image, Hits, showLabel=True)
plt.imshow(Overlay)