Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Write pixel values from image to matrix and display them  (Read 1105 times)

0 Members and 1 Guest are viewing this topic.

LonelyDriver

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Write pixel values from image to matrix and display them
« on: October 31, 2018, 06:44:08 pm »
Hi everyone,

i try to get pixel values from my image and write them into a matrix for further calculus.
For testing i want to display my matrix into another image to make sure i got the right values.
I captured my pixels from the renderwindow into an image. After that i loop through the image to get every pixel value.
Then i wanted to show my matrix into an opencv Mat. All works but my opencv image displays confusing things like an small extract
from the original image. For success it should display the same image like my original.
#include <iostream>
#include <SFML/Graphics.hpp>
#include "opencv2/opencv.hpp"

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape ball(3.f);
    ball.setFillColor(sf::Color::White);

    sf::Vector2f paddleSize(10.f,30.f);
    float ballRadius = 3.f;

    sf::RectangleShape paddle_left(paddleSize);
    sf::RectangleShape paddle_right(paddleSize);
    ball.setRadius(ballRadius);

    paddle_left.setFillColor(sf::Color::Green);
    paddle_right.setFillColor(sf::Color::Blue);
    ball.setFillColor(sf::Color::White);

    paddle_left.setOrigin(paddleSize / 2.f);
    paddle_right.setOrigin(paddleSize / 2.f);
    ball.setOrigin(ballRadius, ballRadius);

    paddle_left.setPosition(sf::Vector2f(195,125));
    paddle_right.setPosition(sf::Vector2f(5,125));
    ball.setPosition(sf::Vector2f(100, 125));

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
            {
            std::cout << "Game will be terminated!\n";
            window.close();
            }
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(ball);
        window.draw(paddle_left);
        window.draw(paddle_right);
        window.display();

         // capture image from Window
    sf::Image image = window.capture();
    // get pixel array
    sf::Color color;
    sf::Vector2u img_size = image.getSize();
    unsigned int arr[img_size.y][img_size.x];
    for(uint y=0;y<img_size.y;y++)
    {
        for(uint x=0;x<img_size.x;x++)
        {
            color = image.getPixel(x,y);

            if (color == sf::Color::Blue)
            {
                arr[x][y] = 0;
            }
            else if(color == sf::Color::Green)
            {
                arr[x][y] = 0;
            }
            else if(color == sf::Color::White)
            {
                arr[x][y] = 0;
            }
            else
            {
                arr[x][y] = 255;
            }
        }
    }
    cv::Mat B_OpenCV(img_size.x, img_size.y,CV_8UC(1), &arr);
    cv::imshow("edges", B_OpenCV);
    if(cv::waitKey(30) >= 0);

    }

    return 0;
}
Do anyone know why i get a wrong image?!

 

LonelyDriver

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Write pixel values from image to matrix and display them
« Reply #1 on: November 02, 2018, 06:57:21 pm »
Ok Guys i found my fail. I had to change the type of the array "arr" to unsigned char. Then opencv shows me the right reconstructed image.  ;D ;D ;D