I get this error when I compile this code in my compiler.
I tested this code in VSCode and I didn't get any error but in 'codeblocks' IDE, I get this error:
ERROR: undefined reference to 'Image::numbers'
This is my code:
note: I don't write those pieces of my code which they are not so important to solve my problem, like 'getWidth() function' etc.
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
class Image
{
private:
static vector < vector < vector<bool> > > numbers;
static void setImageMatrix(ifstream& image, vector< vector< vector<bool> > > &matrix, int index)
{
int width = getWidth(image);
int height = getHeight(image);
int zeros = getNumberOfZeros(width);
int lineCharsNumber = width * 3 + zeros;
char pixelsLine[lineCharsNumber];
int end = width*3;
vector <bool> tempVector;
for(int i = 0; i < height; i++)
{
image.read(pixelsLine, lineCharsNumber);
tempVector.clear();
for (int j = 0; j < end; j+=3)
{
int average = charToInt(pixelsLine[j]) * 0.11 + charToInt(pixelsLine[j+1]) * 0.59
+ charToInt(pixelsLine[j+2]) * 0.3;
if(average < 70)
tempVector.push_back(true);
else
tempVector.push_back(false);
}
matrix[index].push_back(tempVector);
}
}
void static setNumbersMatrix()
{
ifstream zeroPic("F:\\Projects\\C++\\-[Project1]Image_Processing\\Images\\0.bmp", ios::in | ios::binary);
setImageMatrix(zeroPic, numbers, 0);
}
and at last: sorry about my English, I appreciate that if you help me, thanks!
Related
I am trying to initialise two 2D arrays, namely psi_0 and omega_0, with the data type as double. The sizes of the arrays are 401x401. I have written the last printing statement to check if the code is running properly or not. As soon as I initialise arrays, the last line doesn't get printed and this is getting displayed only: PS C:\Users\Avii\Desktop> cd "c:\Users\Avii\Desktop\" ; if ($?) { g++ trialc++.cpp -o trialc++ } ; if ($?) { .\trialc++ }.
Following is my code:
#include<iostream>
#include<iomanip>
#include <math.h>
#include <vector>
#include <algorithm>
#include <thread>
#include <chrono>
using namespace std;
void display(vector<double> &v){
for (int i = 0; i < v.size(); i++)
{
cout<<v[i]<<" ";
}
cout<<endl;
}
int main(){
const int N = 401;
const double pi = 3.141592653589793;
double L = pi/2;
double r = 1.5;
//-------------------------------------------------------------------
.
..
...
double psi_0[N][N];
double omega_0[N][N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
psi_0[i][j] = sin(x_vec[i]) * (sin(y_vec[j]));
omega_0[i][j]= 2 * sin(x_vec[i]) * sin(y_vec[j]);
}
}
cout<< "everything is fine"; // to check if the code has run till last or not!
return 0;
}
Here x_vec and y_vec are two vectors of size 401 and data type as double. I have tried using flush but could not see the problem is solved!
Any help is highly appreciated!!
I'm trying to write my first neural network in C++ and I'm having trouble with it.
Basically I get
segmentation fault 11
So I tried to debug and it turned out that after the line 41 the program stops for a reason called
EXC_BAD_ACCESS,
what does it mean? how can I turn around it? I've seen from other posts that segmentation fault means running out of stack, which would be an issue because that's maybe the 30% of the code that I planned to write
anyway here is my code, I put in the comment what was line 41 (see comment)
#include <vector>
#include <iostream>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <fstream>
#include <sstream>
using namespace std;
struct Connection
{
double w;
double dw;
};
class Neuron;
typedef vector<Neuron> Layer;
//*************************** class Neuron ***************
class Neuron
{
public:
Neuron(unsigned numOutput, unsigned MyIndex);
void setOtpuVal(double Val){m_outputVal = Val; }
double getOutputval(void) const {return m_outputVal; }
void feedForward (const Layer &prevLayer);
private:
static double randomWeight(void) {return (rand()/(RAND_MAX));}
unsigned m_MyIndex;
double m_outputVal;
vector<Connection> m_outputWeights;
};
void Neuron::feedForward (const Layer &prevLayer){
double sum = 0.0; //<===================================that's line 40
for (unsigned i = 0; i < prevLayer.size(); ++i)
{
sum += prevLayer[i].getOutputval() * prevLayer[i].m_outputWeights[m_MyIndex].w;
}
}
Neuron::Neuron(unsigned numOutput, unsigned MyIndex){
for (unsigned c = 0; c < numOutput; ++c)
{
m_outputWeights.push_back(Connection());
m_outputWeights.back().w = randomWeight();
}
m_MyIndex = MyIndex;
}
//*************************** class Net *********************
class Net{
public:
Net (const vector<unsigned> &Topology);
void feedForward(const vector<double> &Input );
void backProp (const vector<double> &Target){};
void getResults(vector<double> &Output) const{}; //const non modifica l'oggetto
private:
std::vector<Layer> m_layer; // m_layer []
};
void Net::feedForward (const vector<double> &Input ) {
//assert(Input.size() == m_layer[0].size()-1);
// assert(inputVals.size() == m_layers[0].size() - 1);
//feeding
for (unsigned i = 0; i < Input.size()-1; ++i) {
m_layer [0] [i].setOtpuVal(Input[i]);
}
//foorward propagatin
for (unsigned i = 0; i < m_layer.size(); ++i)
{
Layer &prevLayer = m_layer[i-1];
for (unsigned j = 0; j < m_layer[i].size(); ++j)
{
m_layer[i][j].feedForward(prevLayer);
}
}
}
Net::Net (const vector<unsigned> &Topology){
unsigned numLayer = Topology.size();
for (unsigned i = 0; i < numLayer; ++i)
{
m_layer.push_back(Layer());
unsigned numOutput = i == Topology.size() - 1 ? 0 : Topology[i +1]; //inportante!!!!
for (unsigned j = 0; j <= Topology[i]; ++j)
{
m_layer.back().push_back(Neuron(numOutput,j));
cout<<"made a Neuron"<<endl;
}
}
}
int main(int argc, char const *argv[])
{
ifstream reader("Istruzioni.txt",ios::in);
std::vector<double> Input;
std::vector<double> Target;
std::vector<double> Output;
std::vector<unsigned> Topology;
Topology.push_back(3);
Topology.push_back(2);
Topology.push_back(1);
Net MyNet(Topology);
char letter;
for (int i = 0; i < 4; ++i)
{
reader.get(letter);
if (i == 3)
{
Target.push_back( (float)letter - 48);
}
else
Input.push_back((float)letter - 48);
}
reader.close();
MyNet.feedForward (Input);
MyNet.backProp(Target);
MyNet.getResults(Output);
return 0;
}
The error message and behavior is typical for an invalid memory access. It is one of the possible symptom of UB. So what can be said hereafter is only a probable explanation, but nothing for sure.
Looking at the code, and assuming it was called with a valid prevLayer argument:
for (unsigned i = 0; i < prevLayer.size(); ++i)
{
sum += prevLayer[i].getOutputval() * prevLayer[i].m_outputWeights[m_MyIndex].w;
}
it is with high probability an access out of bounds. Since i is in principle within bounds, so the trouble come m_MyIndex.
What to do ?
Check if that member is properly initialized.
Check if its value is positive and that it's smaller than prevLayer[i].m_outputWeights.size()
If this doesn't help solve the issue, it means that some other place in the code messed up (e.g. prevLayer already refers to an object that doesn't exist, or some memory corruption happened before and didn't show any consequence until this moment).
I am trying to build a code where I have to declare a large array in the heap.
At the same time I will use the boost library to perform some matrix calculations (as can be seen in Fill a symmetric matrix using an array
).
My limitations here are two : I will deal with large arrays and matrices so I have to declare everything on the heap and I have to work with arrays and not with vectors.
However I am facing a rather trivial for many people problem... When filling the matrix, the last element doesn't get filled in correctly. So although I expect to get
[3,3]((0,1,3),(1,2,4),(3,4,5))
the output of the code is
[3,3]((0,1,3),(1,2,4),(3,4,2.6681e-315))
I am compiling this code in ROOT6. I don't think it's related to that, I am just mentioning it for completion.
A small sample of the code follows
#include <iterator>
#include <iostream>
#include <fstream>
#include </usr/include/boost/numeric/ublas/matrix.hpp>
#include </usr/include/boost/numeric/ublas/matrix_sparse.hpp>
#include </usr/include/boost/numeric/ublas/symmetric.hpp>
#include </usr/include/boost/numeric/ublas/io.hpp>
using namespace std;
int test_boost () {
using namespace boost::numeric::ublas;
symmetric_matrix<double, upper> m_sym1 (3, 3);
float* filler = new float[6];
for (int i = 0; i<6; ++i) filler[i] = i;
float const* in1 = filler;
for (size_t i = 0; i < m_sym1.size1(); ++ i)
for (size_t j = 0; j <= i && in1 != &filler[5]; ++ j)
m_sym1 (i, j) = *in1++;
delete[] filler;
std::cout << m_sym1 << std::endl;
return 0;
}
Any idea on how to solve that?
Arrays and pointers are not objects of class type, they don't have members. You already have a float *, it is filler.
float const* in1 = filler; // adding const is always allowed
I've manged to finally solve it by changing &filler[5] to &filler[6].
So a version that works is seen below
#include <iterator>
#include <iostream>
#include <fstream>
#include </usr/include/boost/numeric/ublas/matrix.hpp>
#include </usr/include/boost/numeric/ublas/matrix_sparse.hpp>
#include </usr/include/boost/numeric/ublas/symmetric.hpp>
#include </usr/include/boost/numeric/ublas/io.hpp>
using namespace std;
int test_boost () {
using namespace boost::numeric::ublas;
symmetric_matrix<double, upper> m_sym1 (3, 3);
float* filler = new float[6];
for (int i = 0; i<6; ++i) filler[i] = i;
float const* in1 = filler;
for (size_t i = 0; i < m_sym1.size1(); ++ i)
for (size_t j = 0; j <= i && in1 != &filler[6]; ++ j)
m_sym1 (i, j) = *in1++;
delete[] filler;
std::cout << m_sym1 << std::endl;
return 0;
}
Running this code yields the following output
[3,3]((0,1,3),(1,2,4),(3,4,5))
I implemented a function which gives you the indices of all non-zero elements of a cv::mat format matrix. I want to identify the white area of this binary image: http://i.stack.imgur.com/mVJ7N.png .
I want to receive the same result as from Matlab's "find" function.My code looks as follows (I hope the experts won't get a heart attack):
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\ml\ml.hpp>
#include <cstring>
#include <iostream>
#include <vector>
using namespace cv;
void findIndex(Mat imgmat, std::vector <std::vector <int> > &indices);
int main(void)
{
String img;
Mat imgmat;
img = "Mask120.png";
imgmat = imread(img);
std::vector <std::vector <int> > indices;
findIndex(imgmat, indices);
return 0;
}
void findIndex(Mat mask, std::vector< std::vector<int> >& indices){
int x_ind[100000]; // ugly, I know
int y_ind[100000];
int k = 0;
for (int i = 0; i < mask.rows; i++) {
for (int j = 0; j < mask.cols; j++) {
if (mask.at<uchar>(i, j) == 255) {
x_ind[k] = j;
y_ind[k] = i;
k = k + 1;
}
}
}
indices.resize(2, std::vector<int>(k));
for (int m = 0; m < k; m++) {
indices[0][m] = x_ind[m];
indices[1][m] = y_ind[m];
}
}
It gives out two vectors as it should, but the results differ to those of Matlab's "find" and are obviously not correct. With find, there are in total around 22000 non-zero elements, this method identifies around 57000. Also, the first index which is marked as non-zero is at i = 119 and j = 561, corresponding to the point (562/120) of my binary image, which is not a white point.
Any ideas on where I have made a mistake is highly appreciated!
I can give a simple solution for your problem
where input cv::Mat is maskMat(it should be CV_8UC1)
The output will be updated in locations vector as Points of x and y co-ordinates
Mat whiteMat = maskMat == 255;
vector<Point> locations;
int count = countNonZero(binaryMat);
if(count < 0)
{
findNonZero(binaryMat,locations);
}
Some higher version of opencv above 3.0
Mat whiteMat = maskMat == 255;
vector<Point> locations;
findNonZero(binaryMat,locations);
Hello i have a code like this:
#include <iostream>
#include <cstdio>
using namespace std;
int main () {
std::string s="fawwaz";
...
}
then i compiled it with G++ using the gnu gcc online installer i've downloaded from gcc.gnu.org, The compilation runs without any errors and warnings, but when i run, an error appears "program a.exe has stopped working".
and the program runs without any error. Then i try to compile the original file (without double backslash infront of string declaration) the program compiled and run succesfully.
Whats the solution? Where's the problem? Is they any way to fix my problem so i can compile my program via command line NOT via Microsoft Visual C++ since it would be faster to compile via command line? :D
Thank you
This is the complete code :
#include <cstdio>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
void Cetak_Puzzle_Start(){
}
int main(int argc, char const *argv[])
{
string s;
ifstream file("input.txt");
vector<vector<int> > Puzzle_Start;
vector<vector<int> > Puzzle_Finish;
int Puzzle_size=0;
/*
* RETRIEVE PUZZLE SIZE
**/
getline(file,s);
for (int i = 0; i < s.length(); ++i)
Puzzle_size= (Puzzle_size*10) + (int) (s[i]-'0');
/*
* Set Zero ukuran 3x3 vector Puzzle start dan Puzzle finish
**/
vector<int> vtemp(Puzzle_size,0);
for (int i = 0; i < Puzzle_size; ++i)
{
Puzzle_Start.push_back(vtemp);
Puzzle_Finish.push_back(vtemp);
}
/*
* RETRIEVE START STATE
**/
getline(file,s);
int m=0,n=0; // dummy var for looping only m:pointer baris, n:pointer kolom,
for (int i = 0; i < s.length(); ++i)
if (n<Puzzle_size){
if (s[i]=',')
n++;
else if (s[i] >= '0' && s[i] <='9')
Puzzle_Start[m][n]= (Puzzle_Start[m][n] * 10) +(int) (s[i]-'0');
else if (s[i] ='B')
Puzzle_Start[m][n]=-1;
}else{
n=0; // Ganti baris
m++;
}
fclose(stdin);
/*
* CETAK PUZZLE
**/
// for (int i = 0; i < Puzzle_Start.size(); ++i){
// for (int j = 0; j < Puzzle_Start[i].size(); ++j)
// printf("%d ",Puzzle_Start[i][j]);
// printf("\n");
// }
return 0;
}
Here's the bug,
if (s[i]=',')
should be
if (s[i]==',')
and
else if (s[i]='B')
should be
else if (s[i]=='B')
Confusing = (assignment) and == (equality) is a very common error to make