1

I am trying to teach myself some basics of Signal Theory, more specifically Image Processing however I am trying to just get the hang of it from first principles (mostly). I have access to an Academic license for Matlab however I would like to learn the principles using C/C++ (as it is free to use) hence my IDE of choice is Qt.

My current project is to read in an image using QImage and then display the red, green and blue "image parts". However I am not sure how to do this using Qt. My idea is as follows; when displaying the red part set all Green and Blue values for all pixels to zero and display the output and then repeat for each colour component.

QImage my_original_image;
my_original_image.load(":/pic.png");

ui->img_label->setPixmap(QPixmap::fromImage(my_original_image));

The above code snippet is what I use to read in an image and display it onto a label and it works for me.

for (int row=0; row<my_original_image.width(); row++)
{
    for (int col=0; col<my_original_image.height(); col++)
    {
        int r = qRed(my_original_image.pixel(row,col));
        qDebug() << "red " << r <<endl;
        std::cout << r << std::endl;
    }
}

The above code snippet is what I have managed to put together from other forums and all it does is display the red value for all the pixels in the picture.

Herewith my question: How can I modify the above code in order to display the red, green and blue "image parts" into a label similar to the 1st code snippet?

cbuchart
  • 10,847
  • 9
  • 53
  • 93
Benjamin Beer
  • 77
  • 1
  • 12
  • "I would like to learn the principles using C/C++". First principle to learn: C and C++ are two different languages, they have similarities, and there can be code that can be compiled as C and as C++, but your code is not valid C (there is no `std::cout` in C) – 463035818_is_not_an_ai Oct 01 '19 at 09:52

1 Answers1

3

What you are looking is to separate the RGB components into individual images.

One way to do so (not very efficient, but that follows your previous code) is this one:

QImage red(img.size(), QImage::Format_ARGB32); // a new image with the same size
for (int row = 0; row < img.height(); ++row) {
  for (int col = 0; col < img.width(); ++col) {
    const auto color = img.pixelColor(col, row);
    const auto c = color.red();
    red.setPixelColor(col, row, QColor(c, 0, 0, c > 0 ? color.alpha() : 0));
  }
}
ui->imgRed->setPixmap(QPixmap::fromImage(red));

The adjust in alpha channel is just for visualization purposes, otherwise you'll have a black color in those pixels where the red component is 0; if that's what you're looking for, replace c > 0 ? color.alpha() : 0 with just the alpha channel of original image color.alpha().

You can repeat the same with the remaining green and blue components.

enter image description here

Adjust the image format and remove alpha channel if not present in your image.

PS: just as a note, you were swapping the meaning col and row variables, you should use rows when traversing from top to bottom (height), and columns when using the width.

cbuchart
  • 10,847
  • 9
  • 53
  • 93
  • Thank you! In between me asking the question and receiving your answer I managed to get a working algorithm however I had the exact problem that you foresaw, the black colour in non red pixels. – Benjamin Beer Oct 01 '19 at 15:54
  • ```c > 0 ? color.alpha() : 0``` what is the purpose of this statement? – Benjamin Beer Oct 01 '19 at 15:55
  • Also why did you use the ```const``` preceding the ```c ``` and ```color``` variables, what purpose does it serve in this context? – Benjamin Beer Oct 01 '19 at 16:01
  • @BenjaminBeer that condition is to preserve the original alpha channel (assuming it exists) when the red component is non-zero, otherwise it set the color to be fully transparent – cbuchart Oct 01 '19 at 20:58
  • @BenjaminBeer the const keyboard is just a custom to set as constants everything that is not intended to be modified later – cbuchart Oct 01 '19 at 21:00
  • Thank you for your excellent response! Does the const improve the operation of the coding? (I apologise if it is a noob question however it is an entirely new concept to me). – Benjamin Beer Oct 01 '19 at 21:15
  • @BenjaminBeer it is basically for correctness, you cannot ensure the compiler will optimize more your code when using const... take a look at: https://stackoverflow.com/a/3435076/1485885 – cbuchart Oct 02 '19 at 06:59