简体   繁体   中英

What I am doing wrong with HSV?

I want to change HSV value of my image. I wrote code below. Unfortunatelly I got that result when I do that:

图片在这里

Is it okay or what I am doing wrong? If you have any question, please write to me. I hope you know what I mean. Thanks!

#include "widget.h"
#include "ui_widget.h"
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;

Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
    image = QImage("D:/Pobrane/Grafika/Hsv (dla obrazka)/1.jpg");
    imagee = QImage("D:/Pobrane/Grafika/Hsv (dla obrazka)/1.jpg");
    bits = image.bits();
    bitss = imagee.bits();
    ui->setupUi(this);
    h=s=v=0;
}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent*)
{
    QPainter p(this);
    QImage pix(bitss, 600, 600, QImage::Format_RGB32);
    p.drawImage(0,0,pix);
}

void Widget::on_H_slider_valueChanged(int value)
{
    update();
    h=value;
    imagee=image;
    for(int i=0; i<imagee.width(); i++)
    {
        for(int j=0; j<imagee.height(); j++)
        {
            QColor color = imagee.pixelColor(i,j);
            if (s!=0 && v!=0) color.setHsv(h, s, v, color.alpha());
            else if (s!=0) color.setHsv(h, s, color.value(), color.alpha());
            else if (v!=0) color.setHsv(h, color.saturation(), v, color.alpha());
            else color.setHsv(h, color.saturation(), color.value(), color.alpha());
            imagee.setPixelColor(i, j, color);
        }
    }
}

void Widget::on_S_slider_valueChanged(int value)
{
    update();
    s=value;
    imagee=image;
    for(int i=0; i<imagee.width(); i++)
    {
        for(int j=0; j<imagee.height(); j++)
        {
            QColor color = imagee.pixelColor(i,j);
            if (h!=0 && v!=0) color.setHsv(h, s, v, color.alpha());
            else if (h!=0) color.setHsv(h, s, color.value(), color.alpha());
            else if (v!=0) color.setHsv(color.hue(), s, v, color.alpha());
            else color.setHsv(color.hue(), s, color.value(), color.alpha());
            imagee.setPixelColor(i, j, color);
        }
    }
}

void Widget::on_V_slider_valueChanged(int value)
{
    update();
    v=value;
    imagee=image;
    for(int i=0; i<imagee.width(); i++)
    {
        for(int j=0; j<imagee.height(); j++)
        {
            QColor color = imagee.pixelColor(i,j);
            if (h!=0 && s!=0) color.setHsv(h, s, v, color.alpha());
            else if (h!=0) color.setHsv(h, color.saturation(), v, color.alpha());
            else if (s!=0) color.setHsv(color.hue(), s, v, color.alpha());
            else color.setHsv(color.hue(), color.saturation(), v, color.alpha());
            imagee.setPixelColor(i, j, color);
        }
    }
}

I have experienced this before. My mistake was I was always modifying the output image.

Make sure that image is always untouched.
All the work should be done on image2 .
The output should also be image2

You can basically connect all your valueChanged() signal to one slot onSliderValueChanged(value) .

Pseudocode:

function onSliderValueChanged(value)
    h = get H slider value
    s = get S slider value
    v = get V slider value
    image2 = image
    for i = 0 to image2.width - 1
       for j = 0 to image2.height - 1
          if h != 0 and s != 0 and v != 0
              // set image2 pixel color
              color = QColor::fromHSV(h, s, v)
              image2.setPixel(i, j, color)

Every slider movement, image2 should always be equal to image at first.
Then make the necessary pixel manipulations on image2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM