Skip to content

Commit

Permalink
Merge pull request #4 from DavidNorthup/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
DavidNorthup authored Oct 23, 2019
2 parents 6ef118e + cb06ff9 commit 4b1a17b
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 23 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,17 @@ This file is meant to be run with an image captured from the webcam you are atte

## Mask Viewer
The current implementation of the software uses opencv to select ranges of pixels defined by HSV value bounds in "Camera.h" for each of the colors on the cube. Opencv iterates through the image once for each color and generates a binary matrix of pixels with value 0 everywhere any given pixel is outside of the bounds for the speicifed color. Then it uses the sampling centers to count the number of pixels in range inside each circle defined by the center and a radius that match each color. The software then counts the number of matches for each color and whichever is highest is determined as the color for that point. This utility allows you to see the masks for a given sample image, and then reveals what it would conclude given the current bounds. Example usage is: ./training-mask.exe [image_file] [sampling locations].


# Installation Details:
1.) The first step is to install opencv, I used [this page](https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html).

2.) Install SFML, I used my package manager to install using: apt-get install libsfml-dev.

3.) Clone this repository and the kociemba repository, and make the target within 'ckociemba'.

4.) Place the executable compiled in hte step prior into the application directory.

5.) Compile all of the executables within the application directory.

6.) Install the arduino software on the boards, with the sketch and libraries included.
12 changes: 11 additions & 1 deletion application/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,27 @@ std::vector<int> CSRImageProcessing::highlightMat(cv::Mat& image, std::vector<cv
return data;
}


/*
This function saves the opencv Mat object into an image specified by the variable path
*/
void CSRImageProcessing::saveImageToFile(std::string path, cv::Mat& image) {
cv::imwrite(path, image);
std::cout << "Saved image to file." << std::endl;
}

/*
This helper function is used by the color reccomendation function. It is used to
determine if the point P is geometrically within the circle given by center and radius.
*/
bool CSRImageProcessing::pointInCircle(cv::Point p, cv::Point center, int radius) {
int dx = p.x - center.x, dy = p.y - center.y;
return std::sqrt(dx*dx + dy*dy) <= radius;
}

/*
This function gives a recommendation for the cube facelet color by examining each of the color
masks. We sample in a circle with center (x,y) with radius rad.
*/
int CSRImageProcessing::getReccomendation(int x, int y, int rad, cv::Mat masks[]) {
int maxCount = 0, maxIndex = 0;
for (int i = 0; i < 6; i++) {
Expand Down
10 changes: 5 additions & 5 deletions application/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
#include <fstream>
#include <vector>

#define RADIUS 7
#define RADIUS 8

#define DATA_ORDER {"BLUE", "GREEN", "ORANGE", "RED", "WHITE", "YELLOW"}
#define COLOR_ORDER {cv::Scalar(255,0,0), cv::Scalar(26,112,0), cv::Scalar(0, 130, 255), cv::Scalar(0,0,255), cv::Scalar(255,255,255), cv::Scalar(0,255,255)}
#define SFML_COLOR_ORDER {sf::Color(0, 0, 255), sf::Color(0, 112,26), sf::Color(255, 130,0), sf::Color(255, 0, 0), sf::Color(255,255,255), sf::Color(255,255,0)}

#define BLUE_LOW cv::Scalar(100, 50, 20)
#define BLUE_LOW cv::Scalar(95, 50, 20)
#define BLUE_HIGH cv::Scalar(140, 250, 250)

#define GREEN_LOW cv::Scalar(40, 30, 0)
#define GREEN_HIGH cv::Scalar(95, 255, 255)
#define GREEN_LOW cv::Scalar(40, 30, 20)
#define GREEN_HIGH cv::Scalar(85, 255, 255)

#define ORANGE_LOW cv::Scalar(15, 40, 40)
#define ORANGE_HIGH cv::Scalar(23, 255, 255)
Expand All @@ -29,7 +29,7 @@
#define RED_HIGH_2 cv::Scalar(255, 200, 200)

#define WHITE_LOW cv::Scalar(0, 0, 60)
#define WHITE_HIGH cv::Scalar(255, 40, 255)
#define WHITE_HIGH cv::Scalar(255, 30, 255)

#define YELLOW_LOW cv::Scalar(23, 40, 20)
#define YELLOW_HIGH cv::Scalar(45, 255, 255)
Expand Down
14 changes: 14 additions & 0 deletions application/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,20 @@ int main(int argc, char *argv[]) {
if (event.key.code == sf::Keyboard::B) {
robot.performMove("B");
}

if (event.key.code == sf::Keyboard::V) {
int counts[] = {0,0,0,0,0,0};
for (int i = 0; i < 54; i++) {
counts[data[i]]++;
}

bool valid = true;
for (int i = 0; i < 6 && valid; i++) {
valid = counts[i] == 9;
}

std::cout << "Valid: " << valid << std::endl;
}
} else if (alt_held) {
if (event.key.code == sf::Keyboard::R) {
robot.performMove("R'");
Expand Down
6 changes: 4 additions & 2 deletions application/DriverSampling.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include <SFML/Graphics.hpp>
#include "Camera.h"
#include <iostream>
#include <fstream>

#include "Camera.h"


int main (int argc, char* argv[]) {
if (argc < 3) {
std::cout << "Not enough args, example use: " << argv[0] << " [sample image file] [facelet name (ex: B9)]" << std::endl;
std::cout << "Not enough args, example use: " << argv[0] << " [sample image file] [sampling center output file]" << std::endl;
return 1;
}
sf::Image image;
Expand Down
11 changes: 5 additions & 6 deletions application/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=csr.exe


all: $(EXECUTABLE)
all: $(EXECUTABLE) sampling-config.exe training-mask.exe

$(EXECUTABLE): $(OBJECTS) *.h
$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) -o $@ $(LDFLAGS)

.cpp.o: $< *.h
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
.cpp.o: $<
$(CC) $(CFLAGS) $< -c $(LDFLAGS)

clean:
rm *.o *.exe


sampling-config.exe: DriverSampling.cpp
$(CC) DriverSampling.cpp -o sampling-config.exe $(LDFLAGS)

training-mask.exe: TrainingTestMask.cpp
training-mask.exe: TrainingTestMask.cpp Camera.o
$(CC) TrainingTestMask.cpp -o training-mask.exe $(LDFLAGS)
1 change: 1 addition & 0 deletions application/TrainingTestMask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ int main (int argc, char* argv[]) {
cv::inRange(hsv, ORANGE_LOW_2 , ORANGE_HIGH_2, orange_mask_2);
cv::inRange(hsv, YELLOW_LOW , YELLOW_HIGH, yellow_mask);
orange_mask = orange_mask | orange_mask_2;
red_mask = red_mask | red_mask_2;

Mat masks[] = {blue_mask, green_mask, orange_mask, red_mask, white_mask, yellow_mask};
Scalar colors[] = COLOR_ORDER;
Expand Down
6 changes: 3 additions & 3 deletions application/options.opt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ Camera_brightness_frd 95
Camera_brightness_flu 100
Camera_brightness_bru 90
Camera_contrast_bld 25
Camera_contrast_frd 25
Camera_contrast_frd 30
Camera_contrast_flu 30
Camera_contrast_bru 20
Camera_contrast_bru 25
Camera_saturation_bld 30
Camera_saturation_frd 30
Camera_saturation_flu 30
Camera_saturation_bru 20
Camera_saturation_bru 30
6 changes: 3 additions & 3 deletions application/training/sampling_locations_bld.dat
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
142 55 L8
152 27 L9
40 127 B3
81 127 B6
128 129 B9
76 122 B6
128 139 B9
143 158 B8
155 184 B7
158 103 D7
171 71 D4
180 42 D1
172 139 D8
181 166 D9
176 171 D9
4 changes: 2 additions & 2 deletions application/training/sampling_locations_bru.dat
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
141 199 U1
141 202 U1
133 167 U2
122 129 U3
134 101 U6
133 105 U6
144 78 U9
92 149 B1
104 186 B2
Expand Down
2 changes: 1 addition & 1 deletion application/training/sampling_locations_frd.dat
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
161 185 D1
154 158 D2
144 124 D3
155 91 D6
155 98 D6
165 63 D9

0 comments on commit 4b1a17b

Please sign in to comment.