What is the simplest method to print star pyramid with minimal code?
It shouldn't use more than one looping statement.
I've produced pyramids with nested loop but I need more leaner code.
const int row = 5;
for(int i = 0;i<row;i++){
stdout.writeln(" "*(row-i)+"* "*i);
}
Related
I'm new to ImageJ and ImageJ macro. I started to make a macro that apply the "Find Maxima" function on an image. Then I would like to test each pixel spotted as maxima to filter them by value. How can I loop through all the points of the selection into my macro ?
Thanks
Finally I found an answer :
threshold = 254;
getSelectionCoordinates(x, y);
for (i=0; i<x.length; i++){
if(getPixel(x[i], y[i]) < threshold){
setKeyDown("alt");
makePoint(x[i], y[i]);
}
due to my current work with OpenVino I have to use OpenCV. I have to convert a std::vector to a cv::Mat-array. My exemplaric code looks like this:
std::vector<float> inputvector(10*10,1.1111);
cv::Mat image = cv::Mat(10,10,CV_32FC1);
for(int i=0;i<10;i++)
{
for (int j=0;j<10;j++)
{
image.at<float>(i,j) = inputvector.at(10*i+j);
}
}
Now I have to wrap my data by Blob::Ptr without allocation of new memory:
Blob::Ptr imgBlob = wrapMat2Blob(image);
For the last line above I get the following error message from OpenVINO inference engine:
Doesn't support conversion from not dense cv::Mat
I do not understand this as my 10*10 array holds the 1.1111-value in every position. Can somebody explain that? Thanks!
I am trying to implement RGB histogram computation for images in Swift (I am new to iOS).
However the computation time for 1500x1000 image is about 66 sec, which I consider to be too slow.
Are there any ways to speed up image traversal?
P.S. current code is the following:
func calcHistogram(image: UIImage) {
let bins: Int = 20;
let width = Int(image.size.width);
let height = Int(image.size.height);
let binStep: Double = Double(bins-1)/255.0
var hist = Array(count:bins, repeatedValue:Array(count:bins, repeatedValue:Array(count:bins, repeatedValue:Int())))
for i in 0..<bins {
for j in 0..<bins {
for k in 0..<bins {
hist[i][j][k] = 0;
}
}
}
var pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage))
var data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)
for x in 0..<width {
for y in 0..<height {
var pixelInfo: Int = ((width * y) + x) * 4
var r = Double(data[pixelInfo])
var g = Double(data[pixelInfo+1])
var b = Double(data[pixelInfo+2])
let r_bin: Int = Int(floor(r*binStep));
let g_bin: Int = Int(floor(g*binStep));
let b_bin: Int = Int(floor(b*binStep));
hist[r_bin][g_bin][b_bin] += 1;
}
}
}
As noted in my comment on the question, there are some things you might rethink before you even try to optimize this code.
But even if you do move to a better overall solution like GPU-based histogramming, a library, or both... There are some Swift pitfalls you're falling into here that are good to talk about so you don't run into them elsewhere.
First, this code:
var hist = Array(count:bins, repeatedValue:Array(count:bins, repeatedValue:Array(count:bins, repeatedValue:Int())))
for i in 0..<bins {
for j in 0..<bins {
for k in 0..<bins {
hist[i][j][k] = 0;
}
}
}
... is initializing every member of your 3D array twice, with the same result. Int() produces a value of zero, so you could leave out the triple for loop. (And possibly change Int() to 0 in your innermost repeatedValue: parameter to make it more readable.)
Second, arrays in Swift are copy-on-write, but this optimization can break down in multidimensional arrays: changing an element of a nested array can cause the entire nested array to be rewritten instead of just the one element. Multiply that by the depth of nested arrays and number of element writes you have going on in a double for loop and... it's not pretty.
Unless there's a reason your bins need to be organized this way, I'd recommend finding a different data structure for them. Three separate arrays? One Int array where index i is red, i + 1 is green, and i + 2 is blue? One array of a custom struct you define that has separate r, g, and b members? See what conceptually fits with your tastes or the rest of your app, and profile to make sure it works well.
Finally, some Swift style points:
pixelInfo, r, g, and b in your second loop don't change. Use let, not var, and the optimizer will thank you.
Declaring and initializing something like let foo: Int = Int(whatever) is redundant. Some people like having all their variables/constants explicitly typed, but it does make your code a tad less readable and harder to refactor.
Int(floor(x)) is redundant — conversion to integer always takes the floor.
If you have some issues about performance in your code, first of all, use Time Profiler from Instruments. You can start it via Xcode menu Build->Profile, then, Instruments app opened, where you can choose Time Profiler.
Start recording and do all interactions in the your app.
Stop recording and analyse where is the "tightest" place of your code.
Also check options "Invert call tree", "Hide missing symbols" and "Hide system libraries" for better viewing profile results.
You can also double click at any listed function to view it in code and seeing percents of usage
I need to compute sum of elements in all columns separately.
Now I'm using:
Matrix cross_corr should be summed.
Mat cross_corr_summed;
for (int i=0;i<cross_corr.cols;i++)
{
double column_sum=0;
for (int k=0;k<cross_corr.rows;k++)
{
column_sum +=cross_corr.at<float>(k,i);
}
cross_corr_summed.push_back(column_sum);
}
The problem is that my program takes quite a long time to run. This is one of parts that is suspicious to cause this.
Can you advise any possible faster implementation???
Thanks!!!
You need a cv::reduce:
cv::reduce(cross_corr, cross_corr_summed, 0, CV_REDUCE_SUM, CV_32S);
If you know that your data is continuous and single-channeled, you can access the matrix data directly:
int width = cross_corr.cols;
float* data = (float*)cross_corr.data;
Mat cross_corr_summed;
for (int i=0;i<cross_corr.cols;i++)
{
double column_sum=0;
for (int k=0;k<cross_corr.rows;k++)
{
column_sum += data[i + k*width];
}
cross_corr_summed.push_back(column_sum);
}
which will be faster than your use of .at_<float>(). In general I avoid the use of .at() whenever possible because it is slower than direct access.
Also, although cv::reduce() (suggested by Andrey) is much more readable, I have found it is slower than even your implementation in some cases.
Mat originalMatrix;
Mat columnSum;
for (int i = 0; i<originalMatrix.cols; i++)
columnSum.push_back(cv::sum(originalMatrix.col(i))[0]);
In my app I would like to randomize set values which I set in #define's. I am looking to use arc4random also. I usually would know how to do this but I have only seen tutorials with very basic things like numbers 0-10!
Any tips/help would be appreciated!
put all of your numbers to an array after that calculate an random number in range of 0 and sizeof your array. After you can get your randomized value from random place of predefined array and remove this value. Do it again for range 0 sizoef array - 1 and so on.
From the Wikipedia objective C article it looks like you can define macros using #define. From their example:
#define Add(x,y) ( x + y )
int a = 1;
int b = 2;
int c = Add(a,b);
NSLog(#"Add result: %i", c);
// this will output
// Add result: 3
I'm not sure how complex you can get with those, but I would think you'd be able to do something like #define MY_VAL() (arc4random()%100) to get a range of values, or maybe even use AlexTeho's idea within the macro.