Related
I'm working on a eye pupil detection project. I found this link for eye center tracking using image gradient method provided in this link.
link- http://thume.ca/projects/2012/11/04/simple-accurate-eye-center-tracking-in-opencv/
He has implemented the idea in c++ and i want to convert it into python code. Everything is going smooth until i get to this void createCornerKernel() function.
cv::Mat *leftCornerKernel;
cv::Mat *rightCornerKernel;
// not constant because stupid opencv type signatures
float kEyeCornerKernel[4][6] = {
{-1,-1,-1, 1, 1, 1},
{-1,-1,-1,-1, 1, 1},
{-1,-1,-1,-1, 0, 3},
{ 1, 1, 1, 1, 1, 1},
};
void createCornerKernels() {
rightCornerKernel = new cv::Mat(4,6,CV_32F,kEyeCornerKernel);
leftCornerKernel = new cv::Mat(4,6,CV_32F);
// flip horizontally
cv::flip(*rightCornerKernel, *leftCornerKernel, 1);
}
How would i convert this cv::mat(4, 6, CV_32F, kEyeCornerKernel) in python?
Any help will be appreciated.
rightCornerKernel = np.array([[-1, -1, -1, 1, 1, 1],
[-1, -1, -1, -1, 1, 1],
[-1, -1, -1, -1, 0, 3],
[1, 1, 1, 1, 1, 1]])
I've been working a bit more on this just to find the solution. As #stormzhou suggested i finally able to solve the problem.
Answering my own question, void createCornerkernels() function in python would be
def createCornerkernels():
leftCornerkernel = None
rightCornerKernel = np.array([[-1, -1, -1, 1, 1, 1],
[-1, -1, -1, -1, 1, 1],
[-1, -1, -1, -1, 0, 3],
[1, 1, 1, 1, 1, 1]])
leftCornerKernel = cv2.flip(rightCornerkernel, 1)
return leftCornerKernel, rightCornerKernel
This question already has answers here:
How to fill OpenCV image with one solid color?
(9 answers)
Closed 4 years ago.
I am new in OpenCV. I have a image what I want is to change the color of each and every pixel of image with any single color.
I found that code but when I run this part of code then a exception is generated.
for (i = 0;i < img1.rows;i++) {
for (j = 0;j < img1.cols;j++) {
img1.at<Vec3b>(i, j) = Vec3b(255, 105, 240);
}
}
Can anyone please tell me the solution.
Or what I found is that this take a lot of time for the conversion So if their is any other approach then please tell me.
// Make a 3 channel image
cv::Mat img(480,640,CV_8UC3);
// Fill entire image with cyan (blue and green)
img = cv::Scalar(255,255,0);
You can use Mat::operator() to select the roi and then assign a value to it.
void main()
{
cv::Mat img = cv::Mat::ones(5, 5, CV_8UC3);
img({2, 4}, {2, 4}) = cv::Vec3b(7, 8, 9);
cout << img;
}
This will print
[ 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0;
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0;
1, 0, 0, 1, 0, 0, 7, 8, 9, 7, 8, 9, 1, 0, 0;
1, 0, 0, 1, 0, 0, 7, 8, 9, 7, 8, 9, 1, 0, 0;
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0]
To fill image with single color, use rectangle with CV_FILLED argument.
(there might be some reasons for exception - image was not created, wrong pixel format etc - it is hard to diagnose a problem with given information)
I came across this:
cv::Mat Mat_out;
cv::Mat Mat2(openFingerCentroids.size(), CV_8UC1, cv::Scalar(2)); imshow("Mat2", Mat2);
cv::Mat Mat3(openFingerCentroids.size(), CV_8UC1, cv::Scalar(3)); imshow("Mat3", Mat3);
cv::bitwise_and(Mat2, Mat3, Mat_out); imshow("Mat_out", Mat_out);
Why does Mat_out contain all 2? Bit-wise operation of a matrix of all 2s and 3s should give me 0, right? Since 2 is not equal to 3?
Anyway, this is the simple thing I tried to implement: (like find function of MATLAB)
Mat_A = {1, 1, 0, 9, 0, 5;
5, 0, 0, 0, 9, 0;
1, 2, 0, 0, 0, 0};
Output expected, if I'm searching for all 5s:
Mat_out = {0, 0, 0, 0, 0, 5;
5, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0 };
How can I do this in OpenCV using C++??
From the official SFML tutorials, The White Box Problem:-
"When you set the texture of a sprite, all it does internally is store a pointer to the texture instance. Therefore, if the texture is destroyed or moves elsewhere in memory, the sprite ends up with an invalid texture pointer." Thus a sprite without a texture will be seen.
I have a class called World. In this class I made a 2d integer array called level and a vector of type Block called blocks. Now I wanted to store the 'Block' objects inside the vector whenever level[ i ][ j ] = 1.
header file of the 'World' class:-
#ifndef WORLD_H
#define WORLD_H
#include <vector>
#include "Block.h"
#include "Grass.h"
#include <SFML/Graphics.hpp>
using namespace std;
class World
{
public:
World();
void draw(sf::RenderWindow *window);
vector<Block> blocks;
private:
int level[12][16];
int wd;
int hi;
};
#endif // WORLD_H
cpp file of the 'World' class :-
#include "World.h"
#include "Grass.h"
#include "Block.h"
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
World::World() : level{
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1},
{1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
}
{
wd = 16;
hi = 12;
int count = 1;
//make a 'Block' object and pass it in the vector when level[ i ][ j ] = 1.
for(int i = 0; i<hi; i++)
{
for(int j = 0; j<wd; j++)
{
if(level[i][j] == 1)
{
Block block(j*50, i*50);
blocks.push_back(block);
}
}
}
}
void World::draw(sf::RenderWindow *window)
{
for(unsigned int i = 0; i<blocks.size(); i++)
{
blocks[i].draw(window);
}
}
The 'Block' class has two members - sf::Texture blockT and sf::Sprite block. It also has a draw(RenderWindow *window) method. This is how the 'Block' class is made :-
header file for block class
#ifndef BLOCK_H
#define BLOCK_H
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
class Block
{
public:
Block(float x, float y);
void draw(sf::RenderWindow *window);
private:
sf::Texture blockT;
sf::Sprite block;
};
#endif // BLOCK_H
cpp file for 'Block' class
#include "Block.h"
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
Block::Block(float px, float py)
{
if(!(blockT.loadFromFile("textures/block.png")))
{
cout<<"Could not load block texture."<<endl;
}
block.setTexture(blockT);
block.setPosition(sf::Vector2f(px, py));
cout<<px<<endl;
cout<<py<<endl;
}
void Block::draw(sf::RenderWindow *window)
{
window->draw(block);
}
When I run the program, in place of blocks, only white box is shown. I don't understand how the texture is getting destroyed. This is what the output looks like :-
As you can see, the white places are sprites each of size 50*50 without any texture.
You should customise copy-construction of blocks so it updates the texture pointer, something like:
Block::Block(const Block& other)
: blockT(other.blockT), block(other.block)
{
block.setTexture(blockT);
}
That will be used when push_back() in the vector forces a resize, and the newly allocated, larger buffer's elements are copy-constructed from the old elements before the latter are "destructed" and deallocated.
It would be a good idea to support the same kind of update for assignment, i.e. operator=(const Block& rhs).
Regarding your "Thus a sprite without a texture will be seen." - it's much more likely that the behaviour is undefined since you're effectively following a pointer to released memory that could get corrupted with new content at any time, and happens to manifest as a lack of texture currently in your testing, but might crash and burn at some other optimisation level, after some minor code changes, on another compiler or OS etc..
Your solution of having a Block class which stores its own instance of an sf::Texture is going to cause you to have duplicate copies of textures hanging around in memory. It's also going to require you to learn and follow the rule of three when you're dealing with your Block objects as per Tony D's answer.
The simpler solution is to have a separate std::map of filenames to sf::Textures into which you can load the textures that you require once, and retrieve everywhere you need them.
// Have one of these, maybe as a member of your World class?
std::map<std::string,sf::Texture> textures;
// load your textures into it ...
Then in your Block class ...
class Block
{
public:
// constructor now takes a reference to a texture map ...
Block(float x, float y,std::map<std::string,sf::Texture>& textures);
And in the implementation you can retrieve the texture you want by its filename, and assign it t the sprite with setTexture.
This question already has answers here:
Error: Assigning to an array from an initializer list
(2 answers)
Closed 9 years ago.
I've checked on SO already for a simple way to fix this error. I didn't get this when compiling on another computer but suddenly now it's not compiling on my PC. Here's the error I'm getting:
Error: Assigning to an array from an initializer list
And here's the code:
int maze[12][12];
void print(bool playing);
int main()
{
printMaze(false);
playGame();
return 0;
}
void print(bool playing)
{
if (!playing) maze = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{2, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1},
{1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 3},
{1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
}
It might also be worth mentioning that I get a warning on the same line:
Warning: Extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
I know that clearly means I have to use one of these two to use extended initializer lists, but have no idea what to do to resolve the matter.
Edit:
Having g++ follow the C++11 ISO C++ language standard in the settings removes the warning, but not the error.
What do your compilations steps look like? The warning is fairly clear: you are trying to use a feature that requires -std=c++11 or -std=gnu++11, and although that is apparently enabled by default, it is possible that you have overridden it (i.e. explicitly turned it off) somehow. You should examine your compilation process closer and make sure you aren't preventing that feature from being allowed.
A workaround is to use the old-style C function memcpy. This will work with older compilers.
int maze[12][12];
void printMaze(bool playing);
int main()
{
printMaze(false);
playGame();
return 0;
}
void printMaze(bool playing)
{
static int maze1[12][12] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{2, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1},
{1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 3},
{1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
if (!playing) memcpy(maze, maze1, 12*12*sizeof(int));
}