Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I do have a class of Matrix and I access to it with two loops and have stored in it all the values I wanted to have in it.
Matrix MatriceJ(width, height);
for (int i=0;i<width;i++)
{
for (int j=0;j<height;j++)
{
MatriceJ.at(i,j)=....
}
}
But now, I would like to store the MatriceJ in an IplImage* for that I can multiply its different elements, one by one, with the other IplImages.
Can you help me with that?
This should get you started. I assume the data to be unsigned char and one channel, please adjust accordingly.
// Create the image
int depth = IPL_DEPTH_8U; // please adjust
int channels = 1; // please adjust
IplImage* img = cvCreateImage(cvSize(width,height), depth, channels);
// Now assume there is a matrix MatriceJ
// Copy the data to our newly created IplImage*
for (int i=0;i<height;i++)
{
uchar* ptr = (uchar*)(img->imageData + i*img->widthStep);
for (int j=0;j<width;j++)
{
ptr[j] = MatriceJ(i,j);
}
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 months ago.
Improve this question
When I am trying to access the bin values of a generated histogram of a greyscale image, I get this assertion failiure:
Error: Assertion failed (elemSize() == sizeof(_Tp)) in cv::Mat::at ... opencv2\core\mat.inl.hpp, line 943
This is the Code Fragment that throws the failiure:
for (int i = 0; i < 256; i++) {
hist.at<float>(i) = (hist.at<float>(i) / pixelAmount) * 255;
}
My main problem is that i dont really understand the problem associated with the assertion failiure
I looked up the OpenCV documentation for Histogram Calculation and they are accessing the histogram values the same way.
Thanks in advance for any advice
I'll assume that you got your hist Mat from another API call, so its type can't be affected at creation time.
The at<T>() method requires you to know the element type of the Mat (say, CV_8U), and to use that same type (uint8_t) in the access.
There are two ways to solve this situation:
get the scalar value (uint8_t), convert the scalar value so it suits your calculation, write back (uint8_t) a coerced value
convert the entire Mat to CV_32F, which is equivalent to float, and then do your operations
First option:
for (int i = 0; i < 256; i++) {
hist.at<uint8_t>(i) =
(static_cast<float>(hist.at<uint8_t>(i)) / pixelAmount) * 255;
}
Second option:
hist.convertTo(hist, CV_32F);
// now do your calculations with at<float>()
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm using OpenGL to load a lot of different textures. As I'm loading a lot of textures though I have around 50 lines that are all calls of the same function, I was just wondering if there is a way to condense this or make it more efficient.
For reference here is what the function call and a section of the code looks like:
//Load the barrier texture image
LoadTexture("barrier/ConcreteBarrier_Texture_stripes.jpg", textureId3, true);
//Load the shed texture image
LoadTexture("shed/shed1.jpg", textureId4, true);
//Load the banana texture image
LoadTexture("banana/Banana_D01.png", textureId5, true);
//Load the UI button 1 image
LoadTexture("ui/button1.png", textureIdB1, false);
//Load the UI button 2 image
LoadTexture("ui/button2.png", textureIdB2, false);
You can put the values into an array, and then loop through the array calling the function, eg:
struct paramInfo {
string file;
GLuint id;
bool b;
};
paramInfo params[] = {
{"barrier/ConcreteBarrier_Texture_stripes.jpg", textureId3, true},
{"shed/shed1.jpg", textureId4, true},
{"banana/Banana_D01.png", textureId5, true},
{"ui/button1.png", textureIdB1, false},
{"ui/button2.png", textureIdB2, false},
...
};
for (auto &p : params) {
LoadTexture(p.file.c_str(), p.id, p.b);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to create a lens flare in OpenGL C++. I referred this video and star rendering code.
double calculateGlowSize(double diameter, double temperature, double distance) {
static const double DSUN = 1392684.0;
static const double TSUN = 5778.0;
// Georg's magic formula
double d = distance; // Distance
double D = diameter * DSUN;
double L = (D * D) * pow(temperature / TSUN, 4.0); // Luminosity
return 0.016 * pow(L, 0.25) / pow(d, 0.5); // Size
}
but i want the output to be more like the image shown below.
How do i create a lens flare effect?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am working on a C++ program which should transfer a 2D image of a flame's intensity into a 3D model. The program is mainly working with multiple matrix-operations which I all realised using pointers (I know, I could use vectors though).
After the Input of the textfile, the mirroring and smoothing of the data values, there comes a correction calculation for each line of the image. At the beginning of the function for this calculation, the program stops on a random position but in the for-loop declaring the y_values-vector.
Here is the code-fragment:
void CorrectionCalculation(Matrix Matrix_To_Calculate, int n_values, int polynomial_degree, int n_rows)
{
for (int h = 0; h < n_rows; h++)
{
//Initialising and declaration of the y_values-vector, which is the copy of each matrix-line. This line is used for the correction-calculation.
double* y_values = new double(n_values);
for (int i = 0; i < n_values; i++)
{
y_values[i] = Matrix_To_Calculate[h][i];
}
//Initialisiing and declaration of the x-values (from 0 to Spiegelachse with stepwidth 1, because of the single Pixels)
double* x_values = new double(n_values);
for (int i = 0; i < n_values; i++)
{
x_values[i] = i;
}
When calculating a single line, the program worked fine. But when I added some code to calculate the whole image, the program stops.
You're not allocating an array of values, but a single element.
Instead of:
double* y_values = new double(n_values);
// ...
double* x_values = new double(n_values);
Change it to
double* y_values = new double[n_values];
//...
double* x_values = new double[n_values];
You should use a vector of doubles rather than array new. That way the memory will be automatically deleted when its no longer needed. E.g.:
#include <vector>
std::vector<double> y_values(y_values);
You're also hiding variables by using variable names the same as the parameters. This can lead to confusion and subtle bugs in code where you're not quite sure which variable is being changed.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to create terrain which takes height values from txt file .
During my search I realized that generally the terrains are created from bmp files or another kind of picture taking values from pixels. I read height values from file into a 2d array.
void File(){
FILE* file = fopen("data.txt", "r"); // data.txt opened with read only mode to get heights
int i,j;
for (i = 0; i < 17; i++){
for (j = 0; j < 21; j++){
fscanf(file, "%d", &data[i][j]);
}
}
fclose(file);
}
and then load these values to vertex to create triangle.
But there are triangles everywhere when I change the x y z values .
The intended project is to create a terrain.
Is there a special way or code to create terrain by using just height values from a txt file?
OpenGL renders primitives like triangles and triangle strips. You'll need to convert your terrain heightmap into primitives that OpenGL understands.
Perhaps this tutorial found by a quick google search can help you.
http://www.codeproject.com/Articles/14154/OpenGL-Terrain-Generation-An-Introduction
Depending on how the data looks in the heightmap, say its in the format of vertex/height.
You could read in a line, say its
v 1.7/2.1/3.7 h 10
store off the vertex and translate that vertex up 10 (height).
Repeat for each vertex and connect them with faces.
If it's just height values then you could substitue in arbitrary verticies for each height value.
I.E.
heightmap:
h 10
h 20
h 30
Then as a function:
void generateTerrain(int length, int width)
{
for(;length>= 0; --length)
{
for(; width >= 0; --width)
{
//read line from file
//store the height in a temp hieght variable
//createVertex(length, width, height);
//store this vertex somewhere for later
}
}
}
There are probably way more efficient ways of doing that, but for simple terrain generation that just popped in my head :)