Python cv2.TM_SQDIFF Examples
The following are 15
code examples of cv2.TM_SQDIFF().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
cv2
, or try the search function
.
Example #1
Source File: TemplateMatchers.py From lackey with MIT License | 8 votes |
def findAllMatches(self, needle, similarity): """ Find all matches for ``needle`` with confidence better than or equal to ``similarity``. Returns an array of tuples ``(position, confidence)`` if match(es) is/are found, or an empty array otherwise. """ positions = [] method = cv2.TM_CCOEFF_NORMED match = cv2.matchTemplate(self.haystack, self.needle, method) indices = (-match).argpartition(100, axis=None)[:100] # Review the 100 top matches unraveled_indices = numpy.array(numpy.unravel_index(indices, match.shape)).T for location in unraveled_indices: y, x = location confidence = match[y][x] if method == cv2.TM_SQDIFF_NORMED or method == cv2.TM_SQDIFF: if confidence <= 1-similarity: positions.append(((x, y), confidence)) else: if confidence >= similarity: positions.append(((x, y), confidence)) positions.sort(key=lambda x: (x[0][1], x[0][0])) return positions
Example #2
Source File: pixelmatch.py From airtest with BSD 3-Clause "New" or "Revised" License | 6 votes |
def locate_img(image, template): img = image.copy() res = cv2.matchTemplate(img, template, method) print res print res.shape cv2.imwrite('image/shape.png', res) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) print cv2.minMaxLoc(res) if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: top_left = min_loc else: top_left = max_loc h, w = template.shape bottom_right = (top_left[0] + w, top_left[1]+h) cv2.rectangle(img, top_left, bottom_right, 255, 2) cv2.imwrite('image/tt.jpg', img)
Example #3
Source File: pixelmatch.py From ATX with Apache License 2.0 | 6 votes |
def locate_img(image, template): img = image.copy() res = cv2.matchTemplate(img, template, method) print res print res.shape cv2.imwrite('image/shape.png', res) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) print cv2.minMaxLoc(res) if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: top_left = min_loc else: top_left = max_loc h, w = template.shape bottom_right = (top_left[0] + w, top_left[1]+h) cv2.rectangle(img, top_left, bottom_right, 255, 2) cv2.imwrite('image/tt.jpg', img)
Example #4
Source File: WatermarkRemover.py From nowatermark with MIT License | 6 votes |
def find_watermark_from_gray(self, gray_img, watermark_template_gray_img): """ 从原图的灰度图中寻找水印位置 :param gray_img: 原图的灰度图 :param watermark_template_gray_img: 水印模板的灰度图 :return: x1, y1, x2, y2 """ # Load the images in gray scale method = cv2.TM_CCOEFF # Apply template Matching res = cv2.matchTemplate(gray_img, watermark_template_gray_img, method) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: x, y = min_loc else: x, y = max_loc return x, y, x + self.watermark_template_w, y + self.watermark_template_h
Example #5
Source File: detect_by_simple_dense_optical_flow.py From open_model_zoo with Apache License 2.0 | 6 votes |
def _run_match_template_on_rect(image, prev_image, rect, increased_rect): subimage = _get_subimage(image, increased_rect) prev_template = _get_subimage(prev_image, rect) match = cv2.matchTemplate(subimage, prev_template, cv2.TM_SQDIFF) min_val, max_val, min_loc, _ = cv2.minMaxLoc(match) dx, dy = min_loc template_h, template_w = prev_template.shape[:2] subimage_h, subimage_w = subimage.shape[:2] v_x = -(subimage_w / 2.) + dx + template_w / 2. v_y = -(subimage_h / 2.) + dy + template_h / 2. v = Point(v_x, v_y) _draw_match(match, min_val, max_val) return v
Example #6
Source File: match.py From kog-money with MIT License | 5 votes |
def match_template1(template, img, plot=False, method=cv2.TM_SQDIFF_NORMED): img = cv2.imread(img, 0).copy() template = cv2.imread(template, 0) w, h = template.shape[::-1] if lib == OPENCV: res = cv2.matchTemplate(img, template, method) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: top_left = min_loc else: top_left = max_loc else: result = match_template(img, template) ij = np.unravel_index(np.argmax(result), result.shape) top_left = ij[::-1] bottom_right = (top_left[0] + w, top_left[1] + h) if plot: cv2.rectangle(img, top_left, bottom_right, 255, 5) plt.subplot(121) plt.imshow(img) plt.title('Detected Point'), plt.xticks([]), plt.yticks([]) plt.subplot(122) plt.imshow(template) plt.show() return top_left, bottom_right
Example #7
Source File: match_template.py From OpenCV-Python-Tutorial with MIT License | 5 votes |
def MatchingMethod(param): global match_method match_method = param ## [copy_source] img_display = img.copy() ## [copy_source] ## [match_template] method_accepts_mask = (cv2.TM_SQDIFF == match_method or match_method == cv2.TM_CCORR_NORMED) if (use_mask and method_accepts_mask): result = cv2.matchTemplate(img, templ, match_method, None, mask) else: result = cv2.matchTemplate(img, templ, match_method) ## [match_template] ## [normalize] cv2.normalize( result, result, 0, 1, cv2.NORM_MINMAX, -1 ) ## [normalize] ## [best_match] minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(result, None) ## [best_match] ## [match_loc] if (match_method == cv2.TM_SQDIFF or match_method == cv2.TM_SQDIFF_NORMED): matchLoc = minLoc else: matchLoc = maxLoc ## [match_loc] ## [imshow] cv2.rectangle(img_display, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 ) cv2.rectangle(result, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 ) cv2.imshow(image_window, img_display) cv2.imshow(result_window, result) ## [imshow] pass
Example #8
Source File: TemplateMatchers.py From lackey with MIT License | 5 votes |
def findBestMatch(self, needle, similarity): """ Find the best match for ``needle`` that has a similarity better than or equal to ``similarity``. Returns a tuple of ``(position, confidence)`` if a match is found, or ``None`` otherwise. *Developer's Note - Despite the name, this method actually returns the **first** result with enough similarity, not the **best** result.* """ method = cv2.TM_CCOEFF_NORMED position = None match = cv2.matchTemplate(self.haystack, needle, method) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(match) if method == cv2.TM_SQDIFF_NORMED or method == cv2.TM_SQDIFF: confidence = min_val if min_val <= 1-similarity: # Confidence checks out position = min_loc else: confidence = max_val if max_val >= similarity: # Confidence checks out position = max_loc if not position: return None return (position, confidence)
Example #9
Source File: wechat_jump.py From wechat_jump_game with MIT License | 5 votes |
def search(img): result = cv2.matchTemplate(img, template, cv2.TM_SQDIFF) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result) cv2.rectangle(img, (min_loc[0], min_loc[1]), (min_loc[0] + template_size[1], min_loc[1] + template_size[0]), (255, 0, 0), 4) return img, min_loc[0] + template_size[1] / 2, min_loc[1] + template_size[0]
Example #10
Source File: CutImageClass.py From water-meter-system-complete with MIT License | 5 votes |
def getRefCoordinate(self, image, template): # method = cv2.TM_SQDIFF #2 method = cv2.TM_SQDIFF_NORMED #1 # method = cv2.TM_CCORR_NORMED #3 method = cv2.TM_CCOEFF_NORMED #4 res = cv2.matchTemplate(image, template, method) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: top_left = min_loc else: top_left = max_loc # bottom_right = (top_left[0] + w, top_left[1] + h) return top_left
Example #11
Source File: test_debug.py From imgaug with MIT License | 5 votes |
def _find_in_image_avg_diff(cls, find_image, in_image): res = cv2.matchTemplate(in_image, find_image, cv2.TM_SQDIFF) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) top_left = min_loc bottom_right = (top_left[0] + find_image.shape[1], top_left[1] + find_image.shape[0]) image_found = in_image[top_left[1]:bottom_right[1], top_left[0]:bottom_right[0], :] diff = np.abs(image_found.astype(np.float32) - find_image.astype(np.float32)) return np.average(diff)
Example #12
Source File: image_template.py From airtest with BSD 3-Clause "New" or "Revised" License | 5 votes |
def find(search_file, image_file, threshold=0.7): ''' Locate image position with cv2.templateFind Use pixel match to find pictures. Args: search_file(string): filename of search object image_file(string): filename of image to search on threshold: optional variable, to ensure the match rate should >= threshold Returns: A tuple like (x, y) or None if nothing found Raises: IOError: when file read error ''' search = _cv2open(search_file) image = _cv2open(image_file) w, h = search.shape[::-1] method = cv2.CV_TM_CCORR_NORMED res = cv2.matchTemplate(image, search, method) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: top_left = min_loc else: top_left = max_loc bottom_right = (top_left[0] + w, top_left[1] + h) middle_point = (top_left[0]+w/2, top_left[1]+h/2) print top_left, bottom_right return middle_point # if len(region_center): # x = int(maxloc[0]+region_center[0]-source_width/2) # y = int(maxloc[1]+region_center[1]-source_height/2) # else: # [x,y] = maxloc # return max_val, [x,y]
Example #13
Source File: wechat_jump.py From wechat_jump_game with Apache License 2.0 | 5 votes |
def search(img): result = cv2.matchTemplate(img, template, cv2.TM_SQDIFF) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result) cv2.rectangle( img, (min_loc[0], min_loc[1]), (min_loc[0] + template_size[1], min_loc[1] + template_size[0]), (255, 0, 0), 4) return img, min_loc[0] + template_size[1] / 2, min_loc[1] + template_size[0]
Example #14
Source File: Fic.py From RENAT with Apache License 2.0 | 5 votes |
def match_template(self,img,template,threshold=u"0.8"): """ Matches a template in an image using TM_CCOEFF_NORMED method Both `img` and `tempalte` are BGR ndarray object. The result is in the the center and boundary of the match. """ _method = cv2.TM_CCOEFF_NORMED gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) gray_template = cv2.cvtColor(template,cv2.COLOR_BGR2GRAY) w,h = gray_template.shape[::-1] res = cv2.matchTemplate(gray_img,gray_template,_method) loc = np.where(res >= float(threshold)) if len(loc[0]) != 0 and len(loc[1]) != 0: min_val,max_val,min_loc,max_loc = cv2.minMaxLoc(res) if _method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: top_left = min_loc else: top_left = max_loc bottom_right = (top_left[0] + w, top_left[1] + h) mx = int((top_left[0] + bottom_right[0])/2) my = int((top_left[1] + bottom_right[1])/2) result = ((mx,my),(top_left[0],top_left[1],bottom_right[0],bottom_right[1])) BuiltIn().log("Found image at %s" % str(result)) else: result = (None,None) BuiltIn().log("WRN: Could not found the template") return result
Example #15
Source File: matching.py From PGSS with GNU General Public License v3.0 | 5 votes |
def pokemon_image_matching(pokemon_image_name, fort_img_name, is_pokemon): pokemon_image = cv2.imread(pokemon_image_name, cv2.IMREAD_UNCHANGED) fort_img = cv2.imread(fort_img_name, 3) if pokemon_image is None or fort_img is None: return 100000.0 croped = pokemon_image[0:256,0:190] height_f, width_f, channels_f = fort_img.shape scale = 147 / 256 * width_f / 133 scaled = cv2.resize(croped, None, fx=scale, fy=scale) scaled_h, scaled_w, scaled_c = scaled.shape channels = cv2.split(scaled) if is_pokemon: scale_crop_fort = width_f / 156 target_x = (16*scale_crop_fort) target_y = (28*scale_crop_fort) fort_img = fort_img[target_x-2:target_x+2+scaled_h, target_y-2:target_y+2+scaled_w] else: scale_crop_fort = width_f / 133 target_x = int(12*scale_crop_fort) target_y = int(24*scale_crop_fort) fort_img = fort_img[target_x-2:target_x+2+scaled_h, target_y-2:target_y+2+scaled_w] scaled_no_alpth = cv2.merge([channels[0], channels[1], channels[2]]) transparent_mask = cv2.merge([channels[3], channels[3], channels[3]]) white_pixels = channels[3].sum()/255 result = cv2.matchTemplate(fort_img, scaled_no_alpth, cv2.TM_SQDIFF, mask=transparent_mask) min_val3, max_val3, min_loc3, max_loc3 = cv2.minMaxLoc(result) min_val3 = min_val3 / white_pixels return min_val3