I am new to Python and openCV so if this is simple I apologize in advance.
I am trying to follow the depth map code at http://altruisticrobot.tistory.com/219.
In the code below ConvertScale raises the following error:
src.size == dst.size && src.channels() == dst.channels()
I have spent a couple days and cant figure out why.
Any pointers would be greatly appreciated.
Thanks in advance.
im_l = cv.LoadImage('C:\Python27\project\captureL6324.png',cv.CV_LOAD_IMAGE_GRAYSCALE)
im_r = cv.LoadImage('C:\Python27\project\captureR6324.png',cv.CV_LOAD_IMAGE_GRAYSCALE)
imsize = cv.GetSize(im_l)
disp = cv.CreateImage(imsize,cv.IPL_DEPTH_16S,1)# to receive Disparity
#run stereo Correspondence-- returns a single channel 16 bit signed disparity
disparity = cv.FindStereoCorrespondenceBM(im_r,im_l,disp,cv.CreateStereoBMState())
#convert to a real disparity by dividing it by 16-- first create variable to hold the converted scale
real_disparity = cv.CreateImage(imsize,cv.IPL_DEPTH_16S,1)
cv.ConvertScale(disparity, real_disparity, -16,0)
#get the point cloud
depth = cv.CreateImage(cv.GetSize(im_l),cv.IPL_DEPTH_32F,3)
cv.ReprojectIMageTo3D(real_disparity,depth, ReprojectMatrix)
Your problem is, that FindStereoCorrespondenceBM saves the result in the third parameter but you are using the return value, which is None for further computation. This leads to an error since a matrix of a certain size and type is expected.
So just change
cv.ConvertScale(disparity, real_disparity, -16,0)
to
cv.ConvertScale(disp, real_disparity, -16,0)
To run the whole script I also changed the last line to
ReprojectMatrix = cv.CreateMat(4,4,cv.CV_32FC1);
cv.ReprojectImageTo3D(real_disparity,depth, ReprojectMatrix)
This script runs without error for me. But I have not checked whether it gives the correct result.
Related
I'm using coordinate_cleaner's country test cc_coun but it's flagging entries with coordinates near to the edges country borders. To try to keep them I wanted to buffer the terrestrial area of countries, essentially expanding their borders a little so that it doesn't flag these entries.
I've tried:
require(rnaturalearth)
world <- ne_countries(scale = "large", returnclass = "sf") %>% st_buffer(dist=.001)
Using st_buffer(dist=.001) does change the geometry, but I noticed whatever I put into dist doesn't matter as it changes it to the same thing regardless (I tried .001, 0.1, 1, 2, and -1, though any minus number removes the geometry altogether).
I found that maybe I need to transform my sf file into something else and then use st_buffer so it's in meters rather than degrees(?). I saw this question/answer but I don't understand it well enough to help my situation: What unit is the `dist` argument in `st_buffer` set to by default?
In the end I'm looking to create a SpatialPolygonsDataFrame reference file to feed into cc_coun. Using the above code I followed on with:
world <- sf:::as_Spatial(world)
coun_test <- cc_coun(x = data,
lon = "Decimal_Long",
lat = "Decimal_Lat",
iso3 = "Country_code",
value = "flagged",
ref = world,
verbose = TRUE)
Which ended up flagging more entries than when I didn't use st_buffer on the reference fine.
In summary, I want to add a buffer to the edge of every country border by around 100 meters in a file I can use as a reference in this test. Is this the best way to go about it or is there a better/easier way? I'd appreciate any advice.
Thank you
In Julia, I have a list of neighbors of a location stored in all_neighbors[loc]. This allows me to quickly loop over these neighbors conveniently with the syntax for neighbor in all_neighbors[loc]. This leads to readable code such as the following:
active_neighbors = 0
for neighbor in all_neighbors[loc]
if cube[neighbor] == ACTIVE
active_neighbors += 1
end
end
Astute readers will see that this is nothing more than a reduction. Because I'm just counting active neighbors, I figured I could do this in a one-liner using the count function. However,
# This does not work
active_neighbors = count(x->x==ACTIVE, cube[all_neighbors[loc]])
does not work because the all_neighbors mask doesn't get interpreted correctly as simply a mask over the cube array. Does anyone know the cleanest way to write this reduction? An alternative solution I came up with is:
active_neighbors = count(x->x==ACTIVE, [cube[all_neighbors[loc][k]] for k = 1:length(all_neighbors[loc])])
but I really don't like this because it's even less readable than what I started with. Thanks for any advice!
This should work:
count(x -> cube[x] == ACTIVE, all_neighbors[loc])
Using Seaborn, I'm trying to generate a factorplot with each subplot showing a stripplot. In the stripplot, I'd like to control a few aspects of the markers.
Here is the first method I tried:
import seaborn as sns
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col="time", hue="smoker")
g = g.map(sns.stripplot, 'day', "tip", edgecolor="black",
linewideth=1, dodge=True, jitter=True, size=10)
And produced the following output without dodge
While most of the keywords were implemented, the hue wasn't dodged.
I was successful with another approach:
kws = dict(s=10, linewidth=1, edgecolor="black")
tips = sns.load_dataset("tips")
sns.factorplot(x='day', y='tip', hue='smoker', col='time', data=tips,
kind='strip',jitter=True, dodge=True, **kws, legend=False)
This gives the correct output:
In this output, the hue is dodged.
My question is: why did g.map(sns.stripplot...) not dodge the hue?
The hue parameter would need to be mapped to the sns.stripplot function via the g.map, instead of being set as hue to the Facetgrid.
import seaborn as sns
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col="time")
g = g.map(sns.stripplot, 'day', "tip", "smoker", edgecolor="black",
linewidth=1, dodge=True, jitter=True, size=10)
This is because map calls sns.stripplot individually for each value in the time column, and, if hue is specified for the complete Facetgrid, for each hue value, such that dodge would loose its meaning on each individual call.
I can agree that this behaviour is not very intuitive unless you look at the source code of map itself.
Note that the above solution causes a Warning:
lib\site-packages\seaborn\categorical.py:1166: FutureWarning:elementwise comparison failed;
returning scalar instead, but in the future will perform elementwise comparison
hue_mask = self.plot_hues[i] == hue_level
I honestly don't know what this is telling us; but it seems not to corrupt the solution for now.
I'm having a problem regarding reading the pixel values (w=30, h=10). Suppose I'm using
int readValue = cvGetReal2D(img,y,x); and
int readValue = data[y*step+x];
Lets say I am trying to access pixel values at w=35, h=5 using the (1) and (2) method.
The (1) will output an error of index out of range. But why (2) does not output an error of index out of range?
After that, I'm trying to use try...catch()...
You have a continuous block of memory of
size = w*h = 300
At w = 35 and h = 5 your equation gives
data[5*30+35] = data[190] < data[300]
so there is no error. If this is c++ then even if your index in data was larger than 299 it wouldn't throw an error. In that case you would be accessing the data beyond its bounds which results in undefined behavior.
I assume cvGetReal2D(img,y,x) is smart enough to tell you that one of your indices is larger than the defined size of that dimension even though it could be resolved to a valid address.
I searched a lot, but I was not able to find any example code, which describes how to use the WEKA HierarchicalClusterer. Using the following C#-code gives me an IllegalArgumentException at "agg.buildClusterer(insts);".
weka.clusterers.HierarchicalClusterer agg = new weka.clusterers.HierarchicalClusterer();
agg.setNumClusters(NumCluster);
/*
Tag[] TAGS_LINK_TYPE = agg.getLinkType().getTags();
agg.setLinkType(new SelectedTag(1, TAGS_LINK_TYPE));
*/
agg.buildClusterer(insts);
for (int i = 0; i < insts.numInstances(); i++)
{
int clusterNumber = agg.clusterInstance(insts.instance(i));
}
The StackTrace says:
at java.util.PriorityQueue..ctor(Int32 initialCapacity, Comparator comparator)
at weka.clusterers.HierarchicalClusterer.doLinkClustering(Int32 , Vector[] , Node[] )
at weka.clusterers.HierarchicalClusterer.buildClusterer(Instances data)
but no Message or InnerException is specified.
The varaible "insts" is an Instances-object, which only holds instances with an equal amount of numerical attributes.
Is anyone able to quickly find my error or please post/link some example code?
Further, is the setting of the LinkType (commented code) correct?
Thanks,
Björn
The HierarchicalClusterer class has a TAGS_LINK_TYPE attribute. So like
agg.setLinkType(new SelectedTag(1, HierarchicalClusterer.TAGS_LINK_TYPE));
will achieve what you are after for setting the linking. Now what on earth does that 1 mean? From the javadocs we see what TAGS_LINK_TYPE contains:
-L Link type (Single, Complete, Average, Mean, Centroid, Ward, Adjusted complete, Neighbor Joining)
[SINGLE|COMPLETE|AVERAGE|MEAN|CENTROID|WARD|ADJCOMLPETE|NEIGHBOR_JOINING]
In general, your code looks ok for the C# case. I see you don't set the distance metric in your example above and maybe you would want to do this? I too use Weka as best I can with C# using IKVM. I have found the dataset allowed for hierarchical clustering is not too large. Maybe your dataset exceeds what WEKA can handled and you would avoid your error if you reduced the size of the dataset?