How to convert between QT-QColor with opencv-cv::Scalar?

1.2k views Asked by At

I am using opencv+Qt(c++) and I want convert a QColor RGB pixel to an opencv BGR pixel:

QColor --> cv::Scalar
cv::Scalar --> QColor 
2

There are 2 answers

1
応振强 On BEST ANSWER

qcolor --> scalar

cv::Scalar qcolor2scalar(QColor color)
{
    int r,g,b;
    color.getRgb(&r, &g, &b);
    return cv::Scalar(b,g,r); // swap RGB-->BGR
}

scalar --> qcolor

QColor scalar2qcolor(cv::Scalar color)
{
    return QColor(color[2],color[1],color[0]); // swap RGB-->BGR
}
0
tfv On

You have not given a programming language. In python, you can use commands such as:

cv2image=cv2.cvtColor(cv2image, cv2.COLOR_BGR2RGB)
cv2image=cv2.cvtColor(cv2image, cv2.COLOR_RGB2BGR)
cv2image=cv2.cvtColor(cv2image, cv2.COLOR_RGB2GRAY)

For C++ the commands are similar.

You may also want to have a look here: How to combine OpenCV with PyQt to create a simple GUI?