inverse homo stuff

This commit is contained in:
Alexander Munch-Hansen 2019-04-23 12:53:37 +02:00
parent cbc2adb73a
commit be7dc71647

View File

@ -10,10 +10,10 @@ using OpenCVForUnity.Calib3dModule;
using OpenCVForUnity.ImgprocModule;
using OpenCVForUnity.ImgcodecsModule;
public class HomographyPoints
public class HomographyAndState
{
public List<List<float>> src_points { get; set; }
public List<List<float>> dst_points { get; set; }
public List<List<float>> homography { get; set; }
public List<List<int>> board { get; set; }
}
public class python_test : MonoBehaviour
@ -23,12 +23,22 @@ public class python_test : MonoBehaviour
int baselineWidth = 1500;
int baselineHeight = 1500;
Mat loadedImage;
GameObject rue;
bool displayWarped = false;
double rueX = 0.51;
double rueY = 0.71;
Mat camImgCopy;
// Start is called before the first frame update
void Start()
{
loadedImage = Imgcodecs.imread("/Users/alexandermunch-hansen/projects/chess_stuff/advancedskrald/whole_boards/boards_for_empty/board_1554285984.187497_rank_4.png");
camImgCopy = loadedImage.clone();
}
@ -39,12 +49,12 @@ public class python_test : MonoBehaviour
OpenCVForUnity.ImgcodecsModule.Imgcodecs.imencode(".png", camImageMat, buffer);
string base64 = Convert.ToBase64String(buffer.toArray());
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "/Users/alexandermunch-hansen/.virtualenvs/cv/bin/python3";
var cmd = "/Users/alexandermunch-hansen/projects/chess_stuff/advancedskrald/adapter.py";
var args = "";
start.Arguments = string.Format("\"{0}\" \"{1}\"", cmd, args);
print(start.Arguments);
start.UseShellExecute = false;// Do not use OS shell
start.CreateNoWindow = true; // We don't need new window
start.RedirectStandardInput = true;
@ -52,6 +62,8 @@ public class python_test : MonoBehaviour
start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
using (Process process = Process.Start(start))
{
using (StreamWriter writer = process.StandardInput)
{
writer.WriteLine(base64);
@ -60,9 +72,6 @@ public class python_test : MonoBehaviour
process.WaitForExit();
string err = process.StandardError.ReadToEnd();
print(err);
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadLine();
@ -95,7 +104,6 @@ public class python_test : MonoBehaviour
print("------------------------------------");
print(loadedImage.size());
var camImgCopy = loadedImage.clone();
outputMat = camImgCopy.clone();
camImageMat = loadedImage.clone();
@ -103,43 +111,82 @@ public class python_test : MonoBehaviour
var pls = runCmd(camImageMat);
print(pls);
HomographyPoints homographyPoints = JsonConvert.DeserializeObject<HomographyPoints>(pls);
print(homographyPoints.src_points);
MatOfPoint2f imagePoints = new MatOfPoint2f();
imagePoints.alloc(homographyPoints.src_points.Count);
print(homographyPoints.src_points.Count);
Mat homography = new Mat(new Size(3, 3), CvType.CV_64F);
MatOfPoint2f dstPoints = new MatOfPoint2f();
dstPoints.alloc(homographyPoints.dst_points.Count);
HomographyAndState homographyAndState = JsonConvert.DeserializeObject<HomographyAndState>(pls);
for (int i = 0; i < homographyPoints.src_points.Count; i++)
print(homographyAndState.homography);
for (int i = 0; i < 3; i++)
{
imagePoints.put(i, 0, (int) homographyPoints.src_points[i][0], (int) homographyPoints.src_points[i][1]);
for (int j = 0; j < 3; j++)
{
homography.put(i, j, homographyAndState.homography[i][j]);
print(homographyAndState.homography[i][j]);
}
}
for (int i = 0; i < homographyPoints.dst_points.Count; i++)
{
dstPoints.put(i, 0, (int) homographyPoints.dst_points[i][0], (int) homographyPoints.dst_points[i][1]);
}
var homography = Calib3d.findHomography(imagePoints, dstPoints, Calib3d.RANSAC);
Imgproc.warpPerspective(camImgCopy, outputMat, homography, new Size(baselineWidth, baselineHeight));
imagePoints.Dispose();
dstPoints.Dispose();
foreach (var row in homographyAndState.board) {
foreach (var lel in row) {
print(lel);
}
}
if (outputMat != null)
//List<Point> points = new List<Point>();
MatOfPoint2f points = new MatOfPoint2f();
points.alloc(16);
int cur_level = 0;
int index = 0;
for (int x = 0; x < 8; x++)
{
cur_level += 165;
foreach (int y in new List<int> { 150, 315 })
{
//points.put(index, 0, cur_level, y);
//index += 1;
Imgproc.circle(outputMat, new Point(cur_level, y), 50, new Scalar(255, 0, 0), -1);
}
}
//Core.perspectiveTransform(points, transformedPoints, homography.inv());
//for (int i = 0; i < 16; i++)
//{
// Imgproc.circle(loadedImage, transformedPoints.get(i, 0), 5, new Scalar(255, 0, 0), -1);
//}
if (displayWarped)
{
camImgCopy = loadedImage.clone();
Mat tmpCopy = camImgCopy.clone();
Imgproc.warpPerspective(outputMat,tmpCopy, homography.inv(), camImgCopy.size());
Core.addWeighted(tmpCopy, 0.80f, camImgCopy, 0.7f, 0.0f, camImgCopy);
displayWarped = false;
}
else
{
displayWarped = true;
MatDisplay.DisplayMat(outputMat, MatDisplaySettings.FULL_BACKGROUND);
}
}
if (!displayWarped)
MatDisplay.DisplayMat(camImgCopy, MatDisplaySettings.FULL_BACKGROUND);
}
}