Trying to understand match_template from skimage.feature - scikit-image

I'm trying to understand someone's code from match_template, but I couldn't understand the below process. Let's say there's a picture he's going to chop several parts. The picture is saved in:
ImagenTotal = np.asarray(Image.open('./redmangos.jpg'))
Then he selects 2 places on that picture and the coordinates are:
puntosinteres = [[189.7038558467742, 111.99546370967738],[211.1748235887097, 187.9696572580645]]
Since match_template needs two arguments - one is the original picture and another is what he's going to use to compare. Then the following process looks like this:
xinteres = int(puntosinteres[0][0])
yinteres = int(puntosinteres[0][1])
radio = 10
imagenband = ImagenTotal[:,:,0]
templateband = ImagenTotal[yinteres - radio : yinteres + radio, xinteres - radio : xinteres + radio, 0]
result= match_template(imagenband, templateband)
result = np.where(result>0.8)
I don't know what he's trying to do on imagenband and templateband. Could someone point me to a direction?
Thank you!

imagenband grabs the 0th channel from ImagenTotal, to get a single grayscale image. templateband grabs a small, 20x20 rectangle from yinteres - radio (radius in Spanish) to yinteres + radio on the rows axis and xinteres - radio to xinteres + radio on the columns axis.
To read more on how indexing works for numpy arrays, you can read the official documentation on indexing here:
https://numpy.org/doc/stable/user/basics.indexing.html#basics-indexing
There are links there for more advanced indexing topics.

Related

Visual Studio - Range chart - Adding background colours per category group

I am using a Range chart to create a Gannt Chart of sorts. I am attempting to do two things:
1) Change the background colour of the range chart based upon one of two Category groups, I have two groups, an outer and an inner, and I would like the background colour to change based on the outer grouping. I have a piece of code that determines a colour but, i'm having trouble applying that to the background. I was hoping interlacing colour would help me, that you aren't able to determine the frequency of the interlacing from what I can tell.
The code I'm using is:
Private colorPalette As String() = { "#C85200", "#FF800E", "#5F9ED1",
"#2CA02C", "#A59D93", "#B8341B", "#352F26", "#F1E7D6", "#E16C56", "#CFBA9B"}
Private count As Integer = 0
Private mapping As New System.Collections.Hashtable()
Public Function GetColor(ByVal groupingValue As String) As String
If mapping.ContainsKey(groupingValue) Then
Return mapping(groupingValue)
End If
Dim c As String = colorPalette(count Mod colorPalette.Length)
count = count + 1
mapping.Add(groupingValue, c)
Return c
End Function
2) Position the X and Y axis labels between the major grid lines. The X axis is a date, and the end user would like the date to be between the two grid lines (representing a day) rather than on each grid line, I've had a look around but can't seem to find anything relevant.
Any help or guidance would be appreciated, thanks!

PIL: Imageobject.save() after drawing completely corrupts images and smurfs the ouput

I have these two functions in my program:
def depict_ph_increase(x,y,color, imobject):
program_print(color)
draw = PIL.ImageDraw.Draw(imobject)
draw.text((x, y),color,(255,255,255))
imobject.save('tmp-out.gif')
im_temp = PIL.Image.open("tmp-out.gif")#.convert2byte()
im_temp = im_temp.resize((930, 340), PIL.Image.ANTIALIAS)
MAP_temp = ImageTk.PhotoImage(im_temp)
map_display_temp = Label(main, image=MAP_temp)
map_display_temp.image = MAP_temp # keep a reference!
map_display_temp.grid(row=4,column=2, columnspan=3)
def read_temp_pixels(temperature_file, rngup, rngdown):
temp_image_object = PIL.Image.open(temperature_file)
(length, width) = get_image_size(temp_image_object)
(rngxleft, rngxright) = rngup
(rngyup,rngydown) = rngdown
print 'the length and width is'
print length, width
hotspots = 5;
for hotspot in range(0,hotspots):
color = "#ffffff"
while color == "#ffffff" or color == "#000000" or color == "#505050" or color == "#969696":
yc = random.randint(rngxleft, rngxright)
xc = random.randint(rngyup,rngydown)
color = convert_RGB_HEX(get_pixel_color(temp_image_object, xc, yc))
depict_ph_increase(xc,yc,color, temp_image_object)
The bottom one calls the top one. Their job is to read in this image:
It then randomly selects a few pixels, grabs their colors, and writes the hex values of the colors on top. But, when it redisplays the image, it gives me this garbage:
Those white numbers up near the upper right corner are the hex values its drawing. Its somehow reading the values from the corrupted image, despite the fact that I don't collect the values until AFTER I actually call the ImageDraw() method. Can someone explain to me why it is corrupting the image?
Some background--the get_pixel_color() function is used several other times in the program and is highly accurate, its just reading the pixel data from the newly corrupted image somehow. Furthermore, I do similar image reading (but not writing) at other points in my code.
If there is anything I can clarify, or any other part of my code you want to see, please let me know. You can also view the program in its entirety at my github here: https://github.com/jrfarah/coral/blob/master/src/realtime.py It should be commit #29.
Other SO questions I have examined, to no avail: Corrupted image is being saved with PIL
Any help would be greatly appreciated!
I fixed the problem by editing this line:
temp_image_object = PIL.Image.open(temperature_file)
to be
temp_image_object = PIL.Image.open(temperature_file).convert('RGB')

Removing Noise From an image in MATLAB

I'm using Geometric mean filter to remove noise instead of median filter as image quality goes off in former case. code shown below is a part of m-file to remove noise.
fname = getappdata(0, 'fname');
[a, map] = imread(fname);
x = ind2rgb(a, map);
b = im2double(x);
w=fspecial('gaussian',[3,3]);
geom=exp(imfilter(log(b),ones(3,3),'replicate')).^(1/3/3);
fname=imfilter(b,w,'replicate');
axes(handles.axes1);
imshow(fname);
If i press push button which is named 'Remove Noise' above code get executed irrespective of image quality/property. In the sense even if no noise is present, image will be subjected to filter.
My question, is there any way to detect whether noise is present or not, so that the moment i pressed push button if no Noise in image it should display a message stating 'NO NOISE TO REMOVE' automatically.
There is no direct way to determine whether an image is noisy or not.
However, you can compare resulting image fname with the input image b in such a way that if their difference is lower than a certain threshold, then it can be decided that denoising operation has not changed image effectively and there is not much noise in the original image like:
threshold_ratio = 0.2; % to be decided experimentally
difference_ratio = sum(sum((fname - b).^2)) / sum(sum(b.^2));
if difference_ratio < threshold_ratio
disp('NO NOISE TO REMOVE');
end

Finding area of the image

I used connected component labeling algorithm (bwconncomp) to label the different parts of a binary image (MATLAB). Now i need to calculate the area of different labels and remove the labels with smaller area. Can i use the default area finding command or is there any specific commands for that in matlab...Help..
From the documentation:
CC = bwconncomp(BW) returns the connected components CC found in BW.
The binary image BW can have any dimension. CC is a structure with
four fields...
The final field in CC is PixelIdxList, which is:
[a] 1-by-NumObjects cell array where the kth element in the cell array is
a vector containing the linear indices of the pixels in the kth object.
You can find the area of each label by looking at the length of the corresponding entry in the cell array. Something like:
areas_in_pixels = cellfun(#length, CC.PixelIdxList);
The PixelIdxList is a cell array, each member of which contains the linear indexes of the pixels present in that connected component. The line of code above finds the length of each cell in the cell array - i.e. the number of pixels in each connected component.
I've used cellfun to keep the code short and efficient. A different way of writing the same thing would be something like:
areas_in_pixels = nan(1, length(CC.PixelIdxList);
for i = 1:length(CC.PixelIdxList)
areas_in_pixels(i) = length(CC.PixelIdxList{i});
end
For each connected component, you can then find the size of that component in pixels by accessing an element in areas_in_pixels:
areas_in_pixels(34) %# area of connected component number 34
If you don't want to write lots of code like above just use built-in functions of MATLAB to detect the area. Label your components and from the properties of the component you can find out the area of that component. Suppose Bw is the binary image:
[B,L] = bwboundaries(Bw,'noholes');
stats = regionprops(L,'Area','perimeter');
for k = 1:length(B)
area(k)=stats.Area;
end
You can make this better still by avoiding the for loop with the following:
[B,L] = bwboundaries(Bw,'noholes');
stats = regionprops(L,'Area','perimeter');
area = [stats.Area];
Best,
-Will

Algorithm for heat map?

I have a list of values each with latitude and longitude. I'm looking to create a translucent heatmap image to overlay on Google Maps. I know there are server side and flash based solutions already, but I want to build this in javascript using the canvas tag.
However, I can't seem to find a concise description of the algorithm used to turn coordinates and values into a heatmap. Can anyone provide or link to one?
Thanks.
The basic idea would be to create a grid and project every lat,lng coord to that grid. I would use a 2D array of ints.
The psuedo-code would be:
for each coord
cell = coord projected to grid
increment cell value
end
for 0 to # of passes
for each row
for each col
if grid[row,col] > 0 then
grid[row,col] += 1
increment_adjacent_cells(row, col)
end
end
end
end
So, the idea is that the higher the int value, the hotter that cell is. increment_adjacent_cells should increment the values in all 8 adjacent cells.
I have tried to solve this in javascript using the canvas element, here is my current result:
http://gist.github.com/346165
I have to fix the gaussian filter and the color mapping, because it doesn't give good results currently.
A faster way of building a heatmap could be to use a queue:
Pseudocode:
Add an element to queue (first in heatmap(x,y, val))
While (!queue.isEmpty())
{
elem = queue.pop()
queue.push(elem.x + 1, elem.y, val-1)
queue.push(elem.x - 1, elem.y, val-1)
queue.push(elem.x, elem.y + 1, val-1)
queue.push(elem.x, elem.y - 1, val-1)
}
This saves on tons of iterations!
Look at this project if you are looking for something that looks more like 'tv weather maps':
https://github.com/optimisme/javascript-temperatureMap

Resources