Skip to content

Commit 0c49f78

Browse files
committed
Added qtSmoothColorTransition to the project.
1 parent cd885b3 commit 0c49f78

File tree

7 files changed

+236
-8
lines changed

7 files changed

+236
-8
lines changed

cvQTcameraGL/cvQTcameraGL.pro

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,22 @@ unix:!mac {
2727
-lopencv_imgproc
2828
}
2929

30-
## OpenCV settings for Mac OS X
30+
## OpenCV settings for Mac OS X with OpenCV 3.1
3131
macx {
3232
message("* Using settings for Mac OS X.")
33-
INCLUDEPATH += /usr/local/include/opencv
3433

35-
LIBS += -L/usr/local/lib/ \
34+
# Solve imwrite() undefined symbol
35+
QMAKE_CXXFLAGS += -stdlib=libc++
36+
37+
INCLUDEPATH += "/usr/local/opt/opencv3/include"
38+
39+
LIBS += -L"/usr/local/opt/opencv3/lib" \
3640
-lopencv_core \
3741
-lopencv_highgui \
38-
-lopencv_imgproc
42+
-lopencv_imgproc \
43+
-lopencv_imgcodecs \
44+
-lopencv_videoio \
45+
-lc++
3946
}
4047

4148
## OpenCV settings for Windows and OpenCV 2.4.2

cvQTcameraGL/widget.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
* https://creativecommons.org/licenses/by-sa/2.5/
99
*/
1010
#include "widget.h"
11-
#include <GL/GLU.h>
12-
11+
#ifndef __APPLE__
12+
#include <GL/GLU.h>
13+
#else
14+
#include <glu.h>
15+
#endif
1316
#include <iostream>
1417
#include <QKeyEvent>
1518
#include <QTimer>

cvQTcameraGL/widget.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
* https://creativecommons.org/licenses/by-sa/2.5/
99
*/
1010
#pragma once
11-
#include <cv.h>
12-
#include <highgui.h>
11+
#include <opencv2/opencv.hpp>
1312
#include <QGLWidget>
1413
#include <QImage>
1514

qtSmoothColorTransition/.gitignore

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# This file is used to ignore files which are generated
2+
# ----------------------------------------------------------------------------
3+
4+
*~
5+
*.autosave
6+
*.a
7+
*.core
8+
*.moc
9+
*.o
10+
*.obj
11+
*.orig
12+
*.rej
13+
*.so
14+
*.so.*
15+
*_pch.h.cpp
16+
*_resource.rc
17+
*.qm
18+
.#*
19+
*.*#
20+
core
21+
!core/
22+
tags
23+
.DS_Store
24+
*.debug
25+
Makefile*
26+
*.prl
27+
*.app
28+
moc_*.cpp
29+
ui_*.h
30+
qrc_*.cpp
31+
Thumbs.db
32+
*.res
33+
*.rc
34+
/.qmake.cache
35+
/.qmake.stash
36+
37+
# qtcreator generated files
38+
*.pro.user*
39+
40+
# xemacs temporary files
41+
*.flc
42+
43+
# Vim temporary files
44+
.*.swp
45+
46+
# Visual Studio generated files
47+
*.ib_pdb_index
48+
*.idb
49+
*.ilk
50+
*.pdb
51+
*.sln
52+
*.suo
53+
*.vcproj
54+
*vcproj.*.*.user
55+
*.ncb
56+
*.sdf
57+
*.opensdf
58+
*.vcxproj
59+
*vcxproj.*
60+
61+
# MinGW generated files
62+
*.Debug
63+
*.Release
64+
65+
# Python byte code
66+
*.pyc
67+
68+
# Binaries
69+
# --------
70+
*.dll
71+
*.exe
72+
73+

qtSmoothColorTransition/heightmap.jpg

53 KB
Loading

qtSmoothColorTransition/main.cpp

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#include <iostream>
2+
#include <QApplication>
3+
#include <QImage>
4+
#include <QLabel>
5+
6+
// http://stackoverflow.com/a/25878225/176769
7+
// http://mycodelog.com/2010/05/16/interpolation/
8+
#if 0
9+
QImage smooth_color_transition(const QImage& input, const QVector<QColor>& color_vec)
10+
{
11+
QImage output(input.size(), input.format());
12+
13+
QColor color_src = color_vec.at(0);
14+
unsigned int r1 = color_src.red();
15+
unsigned int g1 = color_src.green();
16+
unsigned int b1 = color_src.blue();
17+
18+
QColor color_dst = color_vec.at(1);
19+
unsigned int r2 = color_dst.red();
20+
unsigned int g2 = color_dst.green();
21+
unsigned int b2 = color_dst.blue();
22+
23+
for (int x = 0; x < input.width(); x++)
24+
{
25+
for (int y = 0; y < input.height(); y++)
26+
{
27+
QRgb pixel = input.pixel(x, y);
28+
int gray = (qRed(pixel) + qGreen(pixel) + qBlue(pixel)) / 3;
29+
float p = gray / 255.0;
30+
31+
//Resultant Color = RGB(A*(1-P)+X*P, B*(1-P)+Y*P, C*(1-P)+Z*P)
32+
int r = (int) r1 * (1-p) + r2 * p;
33+
int g = (int) g1 * (1-p) + g2 * p;
34+
int b = (int) b1 * (1-p) + b2 * p;
35+
output.setPixel(x, y, qRgb(r, g, b));
36+
}
37+
}
38+
39+
return output;
40+
}
41+
#endif
42+
43+
44+
QImage smooth_color_transition(const QImage& input, const QVector<QColor>& color_vec)
45+
{
46+
if (color_vec.size() < 2)
47+
return QImage();
48+
49+
QImage output(input.size(), input.format());
50+
51+
int num_colors = color_vec.size();
52+
std::cout << "Num colors:" << color_vec.size() << std::endl;
53+
54+
int color_width = 256 / (color_vec.size()-1);
55+
std::cout << "Color width:" << color_width << std::endl;
56+
57+
QVector<QColor> pallete;
58+
for (int c = 1; c < num_colors; c++)
59+
{
60+
QColor color_src = color_vec.at(c-1);
61+
unsigned int r1 = color_src.red();
62+
unsigned int g1 = color_src.green();
63+
unsigned int b1 = color_src.blue();
64+
65+
QColor color_dst = color_vec.at(c);
66+
unsigned int r2 = color_dst.red();
67+
unsigned int g2 = color_dst.green();
68+
unsigned int b2 = color_dst.blue();
69+
70+
for (int i = 0; i < color_width; i++)
71+
{
72+
float p = i / (float)color_width; // percentage
73+
74+
//Resultant Color = RGB(A*(1-P)+X*P, B*(1-P)+Y*P, C*(1-P)+Z*P)
75+
76+
int r = (int) r1 * (1-p) + r2 * p;
77+
int g = (int) g1 * (1-p) + g2 * p;
78+
int b = (int) b1 * (1-p) + b2 * p;
79+
pallete.push_back(qRgb(r, g, b));
80+
}
81+
}
82+
83+
// Color width is a REAL number, therefore pallete might end up a little bit short
84+
for (int i = pallete.size(); i < 256; i++)
85+
pallete.push_back(color_vec[color_vec.size()-1].rgb());
86+
87+
std::cout << "Pallete size:" << pallete.size() << std::endl;
88+
89+
for (int x = 0; x < input.width(); x++)
90+
{
91+
for (int y = 0; y < input.height(); y++)
92+
{
93+
QRgb pixel = input.pixel(x, y);
94+
int gray = (qRed(pixel) + qGreen(pixel) + qBlue(pixel)) / 3;
95+
output.setPixel(x, y, pallete[gray].rgb());
96+
}
97+
}
98+
99+
return output;
100+
}
101+
102+
103+
int main(int argc, char* argv[])
104+
{
105+
QApplication app(argc, argv);
106+
107+
QImage input("/Users/karlphillip/workspace/GraphicsProgramming/qtSmoothColorTransition/heightmap.jpg");
108+
if (input.isNull())
109+
return -1;
110+
111+
QLabel input_label;
112+
input_label.setPixmap(QPixmap::fromImage(input));
113+
input_label.resize(input.width(), input.height());
114+
input_label.show();
115+
116+
QVector<QColor> colors;
117+
colors.push_back(QColor(0, 120, 200)); // dark blue
118+
colors.push_back(QColor(120, 220, 255)); // cyan
119+
colors.push_back(QColor(71, 141, 104)); // dark green
120+
colors.push_back(QColor(106, 168, 107)); // medium green
121+
colors.push_back(QColor(141, 173, 132)); // light green
122+
colors.push_back(QColor(166, 191, 151)); // super light green
123+
colors.push_back(QColor(217, 233, 188)); // sand yellow
124+
colors.push_back(QColor(185, 140, 90)); // light orange
125+
colors.push_back(QColor(162, 90, 34)); // light red
126+
colors.push_back(QColor(144, 61, 45)); // dark red
127+
colors.push_back(QColor(151, 68, 64)); // super dark red
128+
colors.push_back(QColor(156, 146, 170)); // purple-red
129+
colors.push_back(QColor(170, 170, 210)); // purple-white
130+
colors.push_back(QColor(240, 247, 243)); // white
131+
132+
QImage output = smooth_color_transition(input, colors);
133+
134+
QLabel output_label;
135+
output_label.setPixmap(QPixmap::fromImage(output));
136+
output_label.resize(output.width(), output.height());
137+
output_label.show();
138+
139+
output.save("colored.jpg");
140+
141+
return app.exec();
142+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
QT += widgets
2+
3+
SOURCES += \
4+
main.cpp

0 commit comments

Comments
 (0)