Skip to content

Commit

Permalink
Projeto 1/Color Picker
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathbonc committed Jan 21, 2022
1 parent ad37681 commit 3a88fa5
Show file tree
Hide file tree
Showing 22 changed files with 140 additions and 7 deletions.
Binary file modified Learning_OpenCV/.vs/Learning_OpenCV/v17/.suo
Binary file not shown.
Binary file modified Learning_OpenCV/.vs/Learning_OpenCV/v17/Browse.VC.db
Binary file not shown.
Binary file not shown.
Binary file not shown.
39 changes: 39 additions & 0 deletions Learning_OpenCV/Learning_OpenCV/ColorPicker(Project1Aux).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main() {
VideoCapture cam(0);
Mat img,imgHSV, mask;
int hmin = 0, smin = 0, vmin = 0;
int hmax = 0, smax = 0, vmax = 0;

namedWindow("Track Bars", (640, 320));
createTrackbar("Hue Min", "Track Bars", &hmin, 179); //Range do HUE é 0-180
createTrackbar("Hue Max", "Track Bars", &hmax, 179);
createTrackbar("Sat Min", "Track Bars", &smin, 255);
createTrackbar("Sat Max", "Track Bars", &smax, 255);
createTrackbar("Val Min", "Track Bars", &vmin, 255);
createTrackbar("Val Max", "Track Bars", &vmax, 255);

while (1) {
cam.read(img);

cvtColor(img, imgHSV, COLOR_BGR2HSV);

Scalar lowerLimit(hmin, smin, vmin), upperLimit(hmax, smax, vmax);
inRange(imgHSV, lowerLimit, upperLimit, mask);

cout << hmin << ", " << smin << ", " << vmin << ", ";
cout << hmax << ", " << smax << ", " << vmax << endl;

imshow("Image Mask", mask);
imshow("Image", img);
waitKey(1);
}
return 0;
}
4 changes: 2 additions & 2 deletions Learning_OpenCV/Learning_OpenCV/Learning_OpenCV.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="FaceDetection.cpp" />
<Xml Include="Recursos\haarcascade_frontalface_default.xml" />
</ItemGroup>
<ItemGroup>
<Xml Include="Recursos\haarcascade_frontalface_default.xml" />
<ClCompile Include="Projeto1.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="FaceDetection.cpp">
<Filter>Arquivos de Origem</Filter>
</ClCompile>
<Xml Include="Recursos\haarcascade_frontalface_default.xml" />
</ItemGroup>
<ItemGroup>
<Xml Include="Recursos\haarcascade_frontalface_default.xml" />
<ClCompile Include="Projeto1.cpp">
<Filter>Arquivos de Origem</Filter>
</ClCompile>
</ItemGroup>
</Project>
92 changes: 92 additions & 0 deletions Learning_OpenCV/Learning_OpenCV/Projeto1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

Mat img;

vector<vector<int>> newPoints;
vector<vector<int>> Colors{//Conjunto de cores detectáveis (HSVmin, HSVmax)
{124, 24, 0, 141, 156, 255},//Roxo
{100, 88, 82, 127, 226, 189},//Azul Claro
{99, 94, 3, 127, 187, 167},//Azul Escuro
{43, 104, 149, 67, 191, 255},//Verde
{12, 139, 168, 27, 226, 255},//Laranja
{24, 80, 123, 41, 149, 248},//Amarelo
{0, 147, 56, 9, 255, 175} //Vinho
};
vector<Scalar> ColorValues{{255,30,255},{255,100,100},{255,10,10},{0,255,0},{0,100,255},{0,255,255},{0,0,255}};
vector<string> ColorNames{ {"Roxo"},{"Azul Claro"},{"Azul Escuro"},{"Verde"},{"Laranja"},{"Amarelo"},{"Vinho"}};

Point getContours(Mat imgDil) { //Segunda imagem é a image que será desenhada
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;

findContours(imgDil, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
//drawContours(img, contours, -1, Scalar(0, 0, 0), 3);

vector<vector<Point>> conPoly(contours.size()); //Arestas do contorno
vector<Rect> boundRect(contours.size()); //Vetor de retângulos, irá armazenar os retângulos;
Point centerPoint(0, 0);
//Filtros
for (int i = 0; i < contours.size(); i++) {
int area = contourArea(contours[i]);
cout << area << endl;


if (area > 1000) {
float peri = arcLength(contours[i], true);//pegando o perímetro do contorno [i], e se ele é fechado ou năo
approxPolyDP(contours[i], conPoly[i], 0.02 * peri, true);
boundRect[i] = boundingRect(conPoly[i]);
centerPoint.x = boundRect[i].x + boundRect[i].width / 2;
centerPoint.y = boundRect[i].y;
//drawContours(img, conPoly, i, Scalar(255, 30, 255), 3);
//rectangle(img, boundRect[i].tl(), boundRect[i].br(), Scalar(255, 0, 0), 5);
}
}
return centerPoint;
}

vector<vector<int>> findColor(Mat img) {
Mat imgHSV;
cvtColor(img, imgHSV, COLOR_BGR2HSV);

for (int i=0; i < Colors.size(); i++) {
Scalar lowerLimit(Colors[i][0], Colors[i][1], Colors[i][2]); //i indica a cor, neste caso
Scalar upperLimit(Colors[i][3], Colors[i][4], Colors[i][5]);
Mat mask;
inRange(imgHSV, lowerLimit, upperLimit, mask);
//imshow(ColorNames[i], mask);
Point centerPoint = getContours(mask);

if (centerPoint.x != 0 && centerPoint.y != 0) {
newPoints.push_back({ centerPoint.x , centerPoint.y , i });
}
}
return newPoints;
}

void Draw(vector<vector<int>> newPoints, vector<Scalar> ColorValues) {
for (int i = 0; i < newPoints.size(); i++) {
circle(img, Point(newPoints[i][0], newPoints[i][1]), 10, ColorValues[newPoints[i][2]], FILLED);
}
}

int main() {
VideoCapture cap(0);


while (1) {
cap.read(img);

newPoints = findColor(img);
Draw(newPoints, ColorValues);

imshow("Image", img);
waitKey(1);
}
return 0;
}
Binary file not shown.
Binary file modified Learning_OpenCV/Learning_OpenCV/x64/Debug/Learning_OpenCV.ilk
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
 FaceDetection.cpp
 Projeto1.cpp
D:\Documentos\Universidade\Codes\How_to_Code\Learning_OpenCV\Learning_OpenCV\Projeto1.cpp(36,12): warning C4244: 'inicializando': conversão de 'double' para 'int', possível perda de dados
D:\Documentos\Universidade\Codes\How_to_Code\Learning_OpenCV\Learning_OpenCV\Projeto1.cpp(41,15): warning C4244: 'inicializando': conversão de 'double' para 'float', possível perda de dados
Learning_OpenCV.vcxproj -> D:\Documentos\Universidade\Codes\How_to_Code\Learning_OpenCV\x64\Debug\Learning_OpenCV.exe
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified Learning_OpenCV/Learning_OpenCV/x64/Debug/vc143.idb
Binary file not shown.
Binary file modified Learning_OpenCV/Learning_OpenCV/x64/Debug/vc143.pdb
Binary file not shown.
Binary file modified Learning_OpenCV/x64/Debug/Learning_OpenCV.exe
Binary file not shown.
Binary file modified Learning_OpenCV/x64/Debug/Learning_OpenCV.pdb
Binary file not shown.

0 comments on commit 3a88fa5

Please sign in to comment.