I am using raphael js in the following way to create multiple circles or rectangles according to an user input:
var xx =parseFloat(document.getElementById("Fem").value);
for(var i = 0; i < xx; i+=1) {
paper.circle(10 + (20*i) , 20 , 5).attr("fill","#FF2");
}
var xy =parseFloat(document.getElementById("Male").value);
for(var i = 0; i < xy; i+=1) {
paper.rect(35 + (20*i), 15 ,10 , 10 ).attr("fill","#FF2");
//paper.path("M 15 +(20*i) , 420 ,l 0 , -40 z");
}
This does the job more or less as I want to, but I would like to have a vertical line from the top of each shape when the iteration is run. paper.path does not work. Would someone please help. I am using this for the first time
I'm not sure entirely what you're trying to achieve, but you must make your arithmetic operations outside of the quotes...
paper.path("M " + (15 + (20*i)) + ", 15 L 0 , -40 z");
This shows the lines, you can then play around to get them where you want.
Related
I use C++ in Xcode on Mac and a pipe method to communicate with Gnuplot. I am interested in converting my arrays into graphs directly through the program, after I run it. Using
FILE *f = popen("gnuplot -persist", "w");
I open the file and then communicate using fprintf.
Now, I have some data in arrays of interest. w is a "proposed" array of standard normal variables and I intend to check if it is indeed a Gaussian distribution with mean = 0 and variance = 1 .To do that I plot a histogram. After that I want to superimpose a real Gaussian function, which has ex as a x coordinate values and gauss as y coordinate values directly on the histogram. How can I do that?
Here's the code so far:
double start = -4; //min
double end = 4 ; //max
double numberofbins = 100;
double width = (end-start)/numberofbins ;
fprintf (f,
"set ylabel '# of elements'\n"
"set xlabel 'The numbers'\n"
"Min = %g\n" //where binning starts
"Max = %g\n" // where binning ends
"n = %g\n" // the number of bins
"width = 10**(-1)\n" // binwidth; (Max-Min)/n
"bin(x) = width*(floor((x-Min)/width)+0.5) + Min\n"
"f(x)= e**((-x**2)/2) / sqrt(2*pi)\n"
"plot '-' using (bin($1)):(1) smooth freq with boxes,'' u $2:$3 with lines linestyle 1\n",start,end,numberofbins)
for (int i= 0; i < numberofpoints; i++){
fprintf(f, "%g %g %g\n", w[i], ex[i], gauss[i]);
}
fclose(f);
Here is the result if I run the demonstrated code:
As we can see, the binning was successful but the line was omitted and gives the following error:
gnuplot> plot '-' using (bin($1)):(1) smooth freq with boxes,'' u $2:$3 with lines linestyle 1
^
line 100000: column() called from invalid context
I have checked online but nobody is practicing communicating with Gnuplot that way.
If I plot only the 2:3 part (without binning), I get this graph:
Thus, the problem might be with the compatibility of these two plots.
there are different ways to plot "inline" data
plot '-' u 1:2 w lines
1 11
2 22
3 33
e
From gnuplot help special-filenames
If you use both '-' and '' on the same plot command, you'll need to
have two sets of inline data, ...
This means:
plot '-' u 1:2 w boxes, '' u 1:2 w lines
1 11
2 22
3 33
e
1 11
2 22
3 33
e
So, instead, I would generate a datablock in the beginning of your generated command string and reuse the data as many times as you need it during your plotting command.
$Data <<EOD
1 11
2 22
3 33
EOD
plot $Data u 1:2 w boxes, '' u 1:2 w lines
I have solved the problem by creating a second y axis on the same graph and plotting according to it. The code used was:
fprintf (f,
"set xlabel 'The numbers'\n"
"Min = %g\n" //where binning starts
"Max = %g\n" // where binning ends
"n = %g\n" // the number of bins
"width = 10**(-1)\n" // binwidth; (Max-Min)/n
"bin(x) = width*(floor((x-Min)/width)+0.5) + Min\n"
"set ytics 100 nomirror tc lt 1\n"
"set ylabel '# of elements' tc lt 1\n"
"set y2tics 0.4 nomirror tc lt 2\n"
"set y2label 'Theoretical Gaussian' tc lt 2\n"
"plot '-' using (bin($1)):(1) smooth freq with boxes title 'Generator Histogram','-' u 1:2 with l axes x1y2 title 'Theoretical Gaussian (mean=0, std = 1)'\n",start,end,numberofbins) ;
for (int i= 0; i < numberofpoints; i++){
fprintf(f, "%g\n", w[i]);
}
fprintf(f,"e\n");
for (int i= 0; i < numberofpoints; i++){
fprintf(f, "%g %g\n",ex[i], gauss[i]);
}
fprintf(f,"e\n");
fclose(f);
which plots this:
I would like to create my own nonlinear filter in OpenCV using C++, and if I see it correctly, I can use the FilterEngine class to do so. Unfortunately, I'm not really able to follow the documentation of this class. (Link: http://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html#filterengine).
Could someone be so kind to explain the class to me in a little bit more detail?
I'm grateful for every input and every example you can provide me with :-)
.
My specific needs:
1) I would like learn how to create my own nonlinear filters in general.
2) I would like to apply a rank-transform filter to my images:
Meaning: I have a kernel/region and I would like to flag every pixel inside that region with a one if the intensity-value of that (neighbourhood-) pixel is lower than the intensity of the center-pixel. Next, I want to use a simple convolution to save the sum of the transformed region, and store the value at the center-pixel. Let's look at a simple example:
100 120 200 rank-trans. 1 0 0 convolution
110 120 220 --> 1 0 0 --> 2
180 200 200 0 0 0
P.S: I know that I can archive the result of 2) by combining 255 threshold-operations with 255 box-filter operations, and then looping over every pixel and selecting the correct value. However, that seems quite inefficient to me ...
.
Code-Snipped [Edit]:
As I still struggle to understand the FilterEngine(), I started to write my own function for the above-descripted usecase. I would also be happy if you could comment on it to improve its efficiency, as it is quite slow at the moment. (~2sec. for a 1080x1920 image on one CPU-core).
void rankTransform(Mat& out, Mat in, int kernal_size, int borderType) {
// Issue warning if neccessary:
if (kernal_size >= 17) {
std::cout << "Warning, need to change Mat-type. Unsigned short only supports kernels up-to the size of 15x15" << std::endl << std::endl;
};
// First: Get borders around the image:
int border_size = (kernal_size - 1) / 2;
Mat in_incl_border = Mat(1080 + 2 * border_size, 1920 + 2 * border_size, in.depth());
copyMakeBorder(in, in_incl_border, border_size, border_size, border_size, border_size, borderType);
// Second: Loop through the image, conduct a rank transform and
// then sum over the kernel-size:
int start_pixel = 0 + (border_size + 1);
int end_pixel_width = 1920 + border_size;
int end_pixel_height = 1080 + border_size;
int i, j;
int x_1, x_2, y_1;
for (i = start_pixel; i < end_pixel_height; ++i) {
x_1 = i - border_size;
x_2 = i + border_size + 1;
for (j = start_pixel; j < end_pixel_width; ++j) {
y_1 = j - border_size;
out.at<unsigned short>(x_1-1, y_1-1) = static_cast<unsigned short>( (sum( in_incl_border(Range(x_1, x_2), Range(y_1, j + border_size + 1)) < in_incl_border.at<unsigned short>(i, j) )[0])/255 );
};
};
im doing a small program , there are 2 arrays , their sizes are the same , lets say its 5 ,
cin >> W;
for (int Q = 0; Q < W; Q++)
cin >> PR[Q] >> CA[Q];
so , my arrays are filled now ,
(numbers are just exampple it doesnot matter i mean their sizes)
1000 300
750 200
950 852
450 250
471 207
now , to understand it better , lets say that on the right side so it means in CA array there are some numbers , which means minutes , literally , and in PR array these numbers are prices , now i need to find the pair which got the smallest price on one minute , normaly i wanted to divide it , but its slow , so i found a rule
if ad < bc then a/b < c/d
but i am not able to use it in the loop everything else is done i mean definitions etc , i just cannot do this "core" how would it look like ?
i also created 2 loops but couldnot implement it correctly
for (int H = 0; H < W; H++) {
for (int B = 0; B < W; B++) {
}
}
You do not need two loops to use the straightforward linear search:
int best = 0;
for (int i = 1 ; i != W ; i++) {
if (pr[i]*ca[best] < pr[best]*ca[i]) {
best = i;
}
}
Note that the loop starts at index 1, not 0, because we do not need to compare the pr[0], ca[0] pair with itself.
I'm a bit lost at this point. If anyone has some spare time to kill please take a look at this and provide suggestions. Been trying for awhile now to figure this out.
Im having trouble updating the number in the tiles after mouse clicks. Posting the entire code below since its mostly interrelated. Tried to narrow the error down but im not 100 percent positive since I just started learning tkinter. I can get the first square to update by changing the 2-D list to a new create_text method of the canvas problem with this is it leaves all of the previous numbers and id have to do this for the entire 9x9 grid (this is the last line before the else statement in handle_clicks)
There has to be an easier way of updating numbers?
def handle_clicks(self, event):
DX, DY = 100, 100
xclick = self.canvas.canvasx(event.x)
yclick = self.canvas.canvasy(event.y)
if (xclick > BORDER_WIDTH and xclick < BORDER_WIDTH + DX and
yclick > BORDER_WIDTH and yclick < BORDER_WIDTH + DX):
if self.final_list[0][0] < str(9):
val = self.final_list[0][0]
val = int(val)
val += 1
self.final_list[0][0] = val
new_val = self.final_list[0][0]
new_val = str(new_val)
self.final_list[0][0] = new_val
self.id_list[0][0] = self.canvas.create_text(xclick,
yclick,
fill = 'yellow',
text = '%s' %
new_val)
print(self.id_list)
else:
self.seed_value[0][0] -= 1
Full code: http://pastebin.com/2FwaMrdd
There has to be an easier way of updating numbers?
There is, you can call Canvas.itemconfigure to configure the text of the text object on the canvas. In your case, this would be:
self.canvas.itemconfigure(self.id_list[0][1], text=new_val)
I am using the OpenCV library for an image processing project to detect hands. I initialized the image in iplimage, colored it, and then converted it to HSV with cvCvtColor(imageHand,imageHand,CV_BGR2HSV );
I don't know the efficient algorithm so that's my problem. Please check my code:
for( int row = 0; row < imageHand->height; row++ )
{
for ( int col = 0; col < imageHand->width; col++ )
{
h =(imageHand->imageData[imageHand->widthStep * row + col * 3]) ;
s = (imageHand->imageData[imageHand->widthStep * row + col * 3 + 1]);
v = (imageHand->imageData[imageHand->widthStep * row + col * 3 + 2]);
if( h>85)
{
imageHand->imageData[imageHand->widthStep * row + col * 3 ] = 0 ;
imageHand->imageData[imageHand->widthStep * row + col * 3 + 1 ] =0 ;
imageHand->imageData[imageHand->widthStep * row + col * 3 + 2 ] = 0 ;
}
else
{
imageHand->imageData[imageHand->widthStep * row + col * 3 ] = 255 ;
imageHand->imageData[imageHand->widthStep * row + col * 3 + 1 ] = 255 ;
imageHand->imageData[imageHand->widthStep * row + col * 3 + 2 ] = 255 ;
}
}
}
I think the range of the searched h is > 85!?
If you know a better algorithm than please guide me.
If you take a look at this site, Hand detection using opencv, you'll find a similar algorithm to what you're using. I would say that the easiest way of detecting a hand would be through the use of colour (i.e. skin detection). I would definitely recommend looking at the algorithm provided by that site first. There's another part that also goes into gesture recognition, if that's an eventual problem you're going to need to handle.
Other possibilities include:
Background Subtraction
This is very simple and prone to breaking, especially if you're planning on the background changing. But, if you're expecting to only use it in front of, say, a white wall... this could be an easy way of going about it.
Shape Analysis
There has been some success with detecting fingertips using the Generalised Hough Transform. False positives can become a worry, however and efficiency is a worry, particularly in situations with a significant amount of background.
as Ancallan has mentioned hand detection using opencv above, I would like to add some more information on the topic of gesture detection. In that post the author used a method of skin colour segmentation, which has got quite good results under specific circumstances.
a new post of hand gesture detection using openCV has been updated, in which the author used a HAAR classifier to detect closed palm, and the results are much more robust than the former ones. but need to point out that the detection objects are somehow limited as one classifier only works for one gesture.