Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stb_image.h" //Put stb_image.h from https://github.com/nothings/stb/blob/master/stb_image.h along side this file in the same dir
- //compile with g++ -Os getdot.cpp -o getdot. Run with ./getdot <filename>
- #include <queue>
- #include <iostream>
- #include <string>
- #include <ctime>
- struct Image
- {
- int width;
- int height;
- unsigned char* data;
- Image(const std::string& filename)
- {
- int n;
- data=stbi_load(filename.c_str(),&width,&height,&n,3);
- }
- ~Image()
- {
- stbi_free(data);
- }
- };
- struct Pixel
- {
- int x,y,score;
- Pixel(int tx,int ty,int tr,int tg,int tb):
- x(tx),y(ty),score(tr-(tg+tb))
- {}
- bool operator<(const Pixel& o) const
- {
- return score >= o.score;//we want the heap top to be the lowest one on top, so invert
- }
- };
- Pixel getDot(const Image& i)
- {
- std::priority_queue<Pixel> toppixels;
- for(size_t y=0;y < i.height;y++)
- {
- for(size_t x=0;x < i.width;x++)
- {
- size_t index=y*width+x;
- unsigned char *pixel_data=i.data+3*index;
- toppixels.emplace(x,y,pixel_data[0],pixel_data[1],pixel_data[2]);
- if(toppixels.size() >= 10)
- {
- toppixels.pop();
- }
- }
- }
- Pixel avg(0,0,0,0,0);
- size_t n=toppixels.size();
- while(!toppixels.empty())
- {
- Pixel c=toppixels.pop();
- avg.x+=c.x;
- avg.y+=c.y;
- avg.score+=c.score;
- }
- avg.x/=n;
- avg.y/=n;
- avg.score/=n;
- return avg;
- }
- int main(int argc,char** argv)
- {
- Image image_to_load(argv[1]);//the filename is the first argument
- clock_t orig=clock();
- Pixel center=getDot(image_to_load);
- clock_t after=clock();
- clock_t deltams=(after-orig)*1000;
- std::cout << "The pixel center is at (" << avg.x << "," << avg.y << ")" << std::endl;
- std::cout << "Finding it took approximately " << deltams/CLOCKS_PER_SEC << " ms" << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement