wanna do this:
#include <armadillo>
int main(){
arma::mat mat=arma::randn(5,5);
mat.each_col( [](arma::vec& vec){
vec=arma::sort_index(vec);
} );
return 0;
Is there any way to turn a uvec into vec?
Use arma::conv_to<DesiredType>::from(value).
See below
#include <armadillo>
int main(){
arma::mat mat=arma::randn(5,5);
mat.print("mat");
mat.each_col( [](arma::vec& vec){
vec = arma::conv_to<arma::vec>::from(arma::sort_index(vec));
} );
mat.print("mat");
return 0;
}
With this we can convert the output of arma::sort_index, which is an arma::uvec, into an arma::vec and the assignment will work.
Related
i'am tray to print a strings from user to screen the problem i faced that i nedd when user insert (enter)key the program go to new line and user still have the ability to print on screen , but when i insert (enter)the program go to the first line and overwrite the old words here is my code
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
int len;
char searsh_word[10];
char ch;
printf("enter your strings\n");
while(ch!='EOF'){
ch=getch();
printf("%c",ch);
}
puts("\nEnter the word you need to search for : ");
scanf("%s",searsh_word);
len=strlen(searsh_word);
printf("your word length is : %d",len);
return 0;
}
the problem was by useing getch() you can use grtchar() for better result
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
FILE *file;
int main()
{
int len;
char searsh_word[10];
char ch;
file=fopen("test.txt","w");
if(!file){
printf("Error opening file\n");
exit(1);
}else{
printf("enter your strings\n");
}
do{
ch=getchar();
fprintf(file,"%c",ch);
}while(ch!='.');
fclose(file);
puts("\nEnter the word you need to search for : ");
scanf("%s",searsh_word);
len=strlen(searsh_word);
printf("your word length is : %d",len);
return 0;
}
i have recently started using opencv and this one has baffled me.
void saveImageSnippet(char *imageName, int height, int width, void* data, int nChannels) //data is char[height * width]
{
char fName[200]="c:\\testimg\\";
FILE *fptr;
IplImage *img;
sprintf(fName,"%s%s.bmp",fName,imageName);
img = cvCreateImageHeader(cvSize(width, height),8/*depth*/,nChannels);
img->imageData=(unsigned char*)data;
cvSaveImage(fName, img); //Unhandled exception
cvReleaseImage(&img);
}
At cvSaveImage : Unhandled exception at 0x6e8e871d in vc2008_1x.exe: 0xC0000005: Access violation reading location 0x745c3a63.
Is there anything i am not doing right?
now an interesting part,
if i add a couple of unused variables, cvSaveImage works just fine
void saveImageSnippet(char *imageName, int height, int width, void* data, int nChannels)
{
int var1, var2; //unused variables
char fName[200]="c:\\testimg\\";
FILE *fptr;
IplImage *img;
sprintf(fName,"%s%s.bmp",fName,imageName);
img = cvCreateImageHeader(cvSize(width, height),8/*depth*/,nChannels);
img->imageData=(unsigned char*)data;
cvSaveImage(fName, img); //now it works fine
cvReleaseImage(&img);
}
please use opencv's c++ api,
avoid anything, that has iplimages in it (especially here on SO !)
#include "opencv2/highgui.hpp" // no, *not* highgui.h !
using namespace cv;
int main()
{
string fName = format("c:\\testimg\\%s.bmp",imageName);
Mat img = imread( fName );
if ( img.empty() )
return -1;
// your image was not loaded
imsave(fName);
return 0;
}
I want to read a sequence of frames from any folder using openCV. All frames are in sequence i.e. (1).jpg,(2).jpg,....
I tried
VideoCapture cap;
cap.open("Directory/");
for(;;)
{
Mat frame;
cap >> frame;
}
but it doesn't work.
This question has been asked before but i don't know why this answer doesn't work for me.
OpenCV: Reading image series from a folder
do i need to rename the images?.
cap open should be cap.open("Directory/(%02d).jpg"); and you have to rename your images so that they look like (01).jpg,(02).jpg etc so that they have fixed length. if the images are like (001).jpg then you should use `cap.open("Directory/(%03d).jpg");
edit
#include "opencv2/opencv.hpp"
using namespace cv;
int main()
{
VideoCapture cap;
cap.open("imgs/(%02d).jpg");
int i=0;
for(;;)
{
if(i++%37==0)cap=VideoCapture("imgs/(%02d).jpg");//there are 37 frames in the dir
Mat frame;
cap >> frame;
imshow("frame",frame);
if(waitKey(1)==27)
exit(0);
}
return 0;
}
Try to prepare an xml/yaml file with list of names with path to the images, in the desired order. Then load the list as a vector or some resembling structure, and then open them one by one in a loop.
Here the full code to do read a sequence of frames with five zeros in name "frame00000.jpg, frame00001.jpg,.....,frame00010.jpg...) using string concatination idea just like matlab.
#include "stdafx.h"
#include <stdlib.h>
#include <math.h>
#include <opencv/cv.h> // include it to used Main OpenCV functions.
#include <opencv/highgui.h> //include it to use GUI functions.
using namespace std;
using namespace cv;
string intToStr(int i,string path){
string bla = "00000";
stringstream ss;
ss<<i;
string ret ="";
ss>>ret;
string name = bla.substr(0,bla.size()-ret.size());
name = path+name+ret+".jpg";
return name;
}
int main(int, char**)
{
string previous_window = "Previous frame";
string current_window = "Current frame ";
int i=0;
for(int i = 1 ; i< 10 ; i++)
{
Mat Current, Previous;
string Curr_name = intToStr(i,"D:/NU/Junior Scientist/Datasets/egtest04/frame");
string Prev_name = intToStr(i-1,"D:/NU/Junior Scientist/Datasets/egtest04/frame");
Current = imread(Curr_name,1);
Previous = imread(Prev_name,1);
namedWindow(current_window,WINDOW_AUTOSIZE);
namedWindow(current_window,WINDOW_AUTOSIZE);
imshow(current_window,Current);
imshow(previous_window,Previous);
waitKey(0);
}
}
Where "D:/NU/Junior Scientist/Datasets/egtest04/frame" is the path sting.
I'm newer to C++. I have written some code, but when i run it, there's always this:
raised exception class
EAccessViolation with message 'Access
violation at address'
i don't understand this. Would you like to help me solve it? It's important to me. Really, really thank you!
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <math.h>
#include <conio.h>
#define k 2
#define minoffset 0.5
using namespace std;
struct Point
{
double X;
double Y;
};
vector<Point> dataprocess();
void k_means(vector<Point> points,int N);
double getdistance(Point p1,Point p2)
{ double distance;
distance=sqrt((p1.X-p2.X)*(p1.X-p2.X)+(p1.Y-p2.Y)*(p1.Y-p2.Y));
return distance;
}
int getmindis(Point p,Point means[])
{
int i;
int c;
double dis=getdistance(p,means[0]);
for(i=1;i<k;i++)
{
double term=getdistance(p,means[i]);
if(term<dis)
{
c=i;
dis=term;
}
}
return c;
}
Point getmeans(vector<Point> points)
{
int i;
double sumX,sumY;
Point p;
int M=points.size();
for(i=0;i<M;i++)
{
sumX=points[i].X;
sumY=points[i].Y;
}
p.X=sumX/M;
p.Y=sumY/M;
return p;
}
int main()
{ int N;
vector<Point> stars;
stars=dataprocess();
N=stars.size();
cout<<"the size is:"<<N<<endl;
k_means(stars,N);
getch();
}
vector<Point> dataprocess()
{
int i;
int N;
double x,y;
vector<Point> points;
Point p;
string import_file;
cout<<"input the filename:"<<endl;
cin>>import_file;
ifstream infile(import_file.c_str());
if(!infile)
{
cout<<"read error!"<<endl;
}
else
{
while(infile>>x>>y)
{
p.X=x;
p.Y=y;
points.push_back(p);
}
}
N=points.size();
cout<<"output the file data:"<<endl;
for(i=0;i<N;i++)
{
cout<<"the point"<<i+1<<"is:X="<<points[i].X<<" Y="<<points[i].Y<<endl;
}
return points;
}
void k_means(vector<Point> points,int N)
{
int i;
int j;
int index;
vector<Point> clusters[k];
Point means[k];
Point newmeans[k];
double d,offset=0;
bool flag=1;
cout<<"there will be"<<k<<"clusters,input the original means:"<<endl;
for(i=0;i<k;i++)
{
cout<<"k"<<i+1<<":"<<endl;
cin>>means[i].X>>means[i].Y;
}
while(flag)
{
for(i=0;i<N;i++)
{
index=getmindis(points[i],means);
clusters[index].push_back(points[i]);
}
for(j=0;j<k;j++)
{
newmeans[j]=getmeans(clusters[j]);
offset=getdistance(newmeans[j],means[j]);
}
if(offset>d)
{
d=offset;
}
flag=(minoffset<d)?true:false;
for(i=0;i<k;i++)
{
means[i]=newmeans[i];
clusters[i].clear();
}
}
for(i=0;i<k;i++)
{
cout<<"N"<<i+1<<"="<<clusters[i].size()<<endl;
cout<<"the center of k"<<i+1<<"is:"<<means[i].X<<" "<<means[i].Y<< endl;
}
}
You surely have some algo errors in you code. It is difficult to deal with code without input data, that caused an error, but let's try:
First, lets look at function Point getmeans(vector<Point> points)
it is supposed to evaluate mean coordinates for cluster of points: if you pass an empty cluster to this function it will cause an error:
look here -
int M=points.size()
and here -
for(i=0;i<M;i++)
{
sumX=points[i].X;
sumY=points[i].Y;
}
if your cluster is empty than M will be zero and you loop will iterate 2^31 times (until 32 bit integer overflow) and each time you will try to read values of nonexistent vector items
So, You have to test if you vector is not empty before running main function loop and you have to decide which mean values should be assigned for zero cluster (May be you need an additional flag for empty cluster which will be checked before dealing with cluster's mean values)
Then lets examine function int getmindis(Point p,Point means[]) and, also, a place, where we call it:
index=getmindis(points[i],means); clusters[index].push_back(points[i]);
This function assings points to clusters. cluster number is ruled by c variable. If input point doesn't fit to any cluster, function will return uninitialized variable (holding any possible value) which. then is used as vector index of nonexisting element - possible access violation error
You probably have to initialize c to zero in declaration
Tell us when you will be ready with errors described above and also show us a sample input file (one which causes errors, if all datasets cause errors, show us the smallest one)
Usually one would only push 'userdata' when the data isn't any of Lua's standard types (number, string, bool, etc).
But how would you push an actually Function pointer to Lua (not as userdata; since userdata is not executable as function in Lua), assuming the function looks like so:
void nothing(const char* stuff)
{
do_magic_things_with(stuff);
}
The returned value should behave like the returned value from this native Lua function:
function things()
return function(stuff)
do_magic_things_with(stuff)
end
end
Is this possible to do with the C API? If yes, how (Examples would be appreciated)?
EDIT: To add some clarity, The value is supposed to be returned by a function exposed to Lua through the C API.
Use lua_pushcfunction
Examples are included in PiL
Here is an example that follows the form of the currently accepted answer.
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdio.h>
/* this is the C function you want to return */
static void
cfunction(const char *s)
{
puts(s);
}
/* this is the proxy function that acts like cfunction */
static int
proxy(lua_State *L)
{
cfunction(luaL_checkstring(L, 1));
return 0;
}
/* this global function returns "cfunction" to Lua. */
static int
getproxy(lua_State *L)
{
lua_pushcfunction(L, &proxy);
return 1;
}
int
main(int argc, char **argv)
{
lua_State *L;
L = luaL_newstate();
/* set the global function that returns the proxy */
lua_pushcfunction(L, getproxy);
lua_setglobal(L, "getproxy");
/* see if it works */
luaL_dostring(L, "p = getproxy() p('Hello, world!')");
lua_close(L);
return 0;
}
You could return a userdata with a metatable that proxies your C function through the __call metamethod. That way the userdata could be called like a function. Below is a full program example.
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdio.h>
/* this is the C function you want to return */
static void
cfunction(const char *s)
{
puts(s);
}
/* this is the proxy function that will be used as the __call metamethod */
static int
proxy(lua_State *L)
{
luaL_checkudata(L, 1, "proxy");
cfunction(luaL_checkstring(L, 2));
return 0;
}
/* this global function returns the C function with a userdata proxy */
static int
getproxy(lua_State *L)
{
lua_newuserdata(L, sizeof (int));
luaL_getmetatable(L, "proxy");
lua_setmetatable(L, -2);
return 1;
}
int
main(int argc, char **argv)
{
lua_State *L;
L = luaL_newstate();
/* create the proxy metatable */
luaL_newmetatable(L, "proxy");
lua_pushcfunction(L, proxy);
lua_setfield(L, -2, "__call");
/* set the global function that returns the proxy */
lua_pushcfunction(L, getproxy);
lua_setglobal(L, "getproxy");
/* see if it works */
luaL_dostring(L, "p = getproxy() p('Hello, world!')");
lua_close(L);
return 0;
}
In retrospect, I completely over-thought what you are asking. All you really need to do is to create a function of type lua_CFunction that pulls the parameters from the Lua stack and passes them on to the target C function. The code above answers your question literally, but it is probably overkill for what you really need to accomplish.