Ask Your Question

alkasm's profile - activity

2017-08-29 16:45:46 -0600 received badge  Enthusiast
2017-07-18 08:35:17 -0600 received badge  Teacher (source)
2017-07-13 07:14:51 -0600 commented answer Template matching-multiple objects

Yeah that's a pretty numpy-python specific operation. It returns the pixel locations where the result is less than threshold...no idea what a C# implementation would look like for that line.

2017-07-13 05:11:52 -0600 answered a question How do i convert a Grayscale video to a Tensor?

First off, check out the OpenCV docs for the Mat class. It shows a bunch of different ways to access, copy, create Mats.

The easiest way would be to use the built-in OpenCV function merge(). Normally, this is used to merge three single channel images into a 3-channel image. For instance, if you split the red, green, and blue channels, process them, and then put them back into a single matrix. But it can actually be used on an arbitrary number of channels, and it accepts a vector of matrices to be merged. So in your for loop, you can push your grayscale image into your vector<Mat>, and then after the loop you can merge() all those matrices into a new matrix.

2017-07-13 04:54:03 -0600 commented question How do i convert a Grayscale video to a Tensor?

To be clear, do you mean you want to stack the grayscale frames into a 3d matrix? Why not just use a vector/list of matrices?

2017-07-13 04:50:32 -0600 answered a question How to find the exact colour range in HSV?

I recently answered a question that dove in deep to inRange() on Stack Overflow. It goes into what the HSV colorspace is, how to think about the values, and how to create your own inRange() function.

For finding your own values to work on an image, it's probably best practice to create a program which shows trackbars for the min and max values of each color channel and adjust them yourself till you get the hang of it. Or just pop an image in the program whenever you need to. I created a python program that does this; you can grab it from GitHub if you want.

This is what it looks like in use:

cspaceFilter in use

2017-07-13 04:43:37 -0600 answered a question Template matching-multiple objects

minMaxLoc() simply gives you the absolute minimum and maximum values, so you cannot grab multiple values with this function. You can do a couple different tricks to grab multiple values. The best idea is not to grab simply the 10 lowest (or highest, depending on method) pixels locations if you have 10 objects, because wherever the minimum and maximum exist, the pixel neighbors of that will probably have similar high/low values too. Instead, the typical method is to define a threshold, where all the values below (or above, depending on method) the threshold count as a "detection". Of course, this threshold will depend on the image, and template, and method---so it's not immediately obvious how to define the right number. However, you have some help---namely, you know the value of the min/max from minMaxLoc(), so you can let this define your threshold. For example, if you were trying to find the minimum of the following array:

[5, 8, 3, 9, 1, 4, 7, 1.1]

You would get 1 as the minimum and 4 as the index of the minimum from minMaxLoc(). Well, if you wanted to get numbers close to that minimum, you could grab the minimum, maybe multiply it to give a little more leeway, and find all values less than that. For this example, maybe you'd multiply your minimum by 1.5, and then find all the values and indices of the array under 1.5.

So specifically, find minMaxLoc() multiply your minimum by some number over 1 (or your maximum by some number under 1) to give a little bit of leeway, and find all elements (by searching or any other method you have available) less than/greater than that multiplied min/max.

Here's a short python program which accomplishes what you're after. Sorry for not being in the language you want, but it should be fairly easy to translate:

import cv2
import numpy as np

# read image
img = cv2.imread('01.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
template = cv2.imread('block.png',0)

# run template matching, get minimum val
res = cv2.matchTemplate(gray, template, cv2.TM_SQDIFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

# create threshold from min val, find where sqdiff is less than thresh
min_thresh = (min_val + 1e-6) * 1.5
match_locations = np.where(res<=min_thresh)

# draw template match boxes
w, h = template.shape[::-1]
for (x, y) in zip(match_locations[1], match_locations[0]):
    cv2.rectangle(img, (x, y), (x+w, y+h), [0,255,255], 2)

# display result
cv2.imshow('', img)
cv2.waitKey(0)

Yielding the following image:

Multiple template matches

Notice that in my program I added a small number (1e-6 or 0.000001) to the number before multiplying in case the minimum was 0 (which it was in my case, since the match is exact---the template was taken directly from the image). If you're working with real images where template isn't a strictly exact copy of a part of the image, then you can probably neglect that ... (more)

2017-07-13 04:09:36 -0600 commented answer image ROIs in python

Not to be too picky on your wording but the indexing for vectors is i:j means from i to j-1. I.e. h, w = img.shape[:2]; print((img[0:h, 0:w] == img).all()) # True while h and w are beyond the indices of the image.