[英]How to recognize the stars in the photo of the night sky using OpenCV?
I want to write a program that will recognize stars in the photo of the night sky and mark them.我想编写一个程序来识别夜空照片中的星星并标记它们。 I'm new in DSP and would like to ask how to implement this.
我是 DSP 的新手,想问一下如何实现这一点。 I sketched a draft code:
我草拟了一个代码草案:
#include "main.hpp"
using namespace std;
//using namespace cv;
int main(int argc, char *argv[])
{
const char *imageName = (argc >= 2) ? argv[1] : "4.jpg";
int64_t processTime;
cv::Mat imageSource, result1, result2, grayScaledImage;
/// Create and initialize the kerenel matrix
cv::Mat kernelMatrix = (cv::Mat_<char>(3, 3) << 0, -1, 0,
-1, 5, -1,
0, -1, 0);
cout << "Start..." << endl;
if ((argc == 3) && !(strcmp("G", argv[2]))) imageSource = cv::imread(cv::samples::findFile(imageName), cv::IMREAD_GRAYSCALE);
else imageSource = cv::imread(cv::samples::findFile(imageName), cv::IMREAD_COLOR);
/// Check for errors while open
if (imageSource.empty())
{
cerr << "Can't open image [" << imageName << "]" << endl;
exit(EXIT_FAILURE);
}
processTime = cv::getTickCount(); /// Start timer
//img_proc::sharpening(imageSource, result1); /// Process image
cv::cvtColor(imageSource, grayScaledImage, cv::COLOR_BGR2GRAY);
cv::filter2D(grayScaledImage, result1, imageSource.depth(), kernelMatrix); /// Process image
processTime = (cv::getTickCount() - processTime) / cv::getTickFrequency(); /// Stop timer and fix the process time
/// Create windows for pictures
cv::namedWindow("Source image", cv::WINDOW_AUTOSIZE);
cv::namedWindow("Result image (1)", cv::WINDOW_AUTOSIZE);
/// Show images
cv::imshow("Source image", imageSource);
cv::imshow("Result image (1)", result1);
cv::waitKey(0);
/// The second method
processTime = cv::getTickCount(); /// Start timer
cv::Sobel(result1, result2, CV_32F, 1, 0); /// S
processTime = (cv::getTickCount() - processTime) / cv::getTickFrequency(); /// Stop timer and fix the process time
cv::namedWindow("Result image (2)", cv::WINDOW_AUTOSIZE);
cv::imshow("Result image (2)", result2);
cv::waitKey();
double minVal, maxVal;
cv::Mat newMat;
cv::minMaxLoc(result2, &minVal, &maxVal);
result2.convertTo(newMat, CV_8U, (255.0 / (maxVal - minVal)), (-minVal * 255.0 / (maxVal - minVal)));
cv::namedWindow("newmat", cv::WINDOW_AUTOSIZE);
cv::imshow("newmat", newMat);
cv::waitKey();
return 0;
}
As a result I get the following images (on the left - the initial image, on the right - the final image):结果,我得到以下图像(左侧 - 初始图像,右侧 - 最终图像):
But I want to highlight the stars, for example, with a yellow circle on a grayscale image.但我想突出星星,例如,在灰度图像上用黄色圆圈。 How I can do it?
我该怎么做?
Here is a MATLAB solution:这是 MATLAB 解决方案:
I = imread('Stars.jpg'); %Read image from file
I = I(:, 1:floor(size(I,2)/2-40), :); %Crop the relevant part (left side).
J = rgb2gray(I); %Convert RGB to Grayscale.
BW = imbinarize(I); %Convert to binary image (you have the option to manually select the threshold).
BW2 = bwmorph(BW2, 'shrink', Inf); %Shrink clusters until single pixel is left.
%The shrink operation wasn't good enough, fill hols, and shrink again.
BW3 = bwfill(BW2, 'holes');
BW4 = bwmorph(BW3, 'shrink', Inf);
%Find coordinates where pixel value is 1
[Y, X] = find(BW4);
%Mark each coordinate with yellow circle.
K = insertMarker(I, [X Y], 'o', 'size', 5, 'color', 'yellow');
figure;imshow(K); %Display result.
Sorry for using MATLAB, but with OpenCV it's going to take me 100 hours...很抱歉使用 MATLAB,但使用 OpenCV 我需要 100 小时...
MATLAB has a profound documentation, so you can learn about each operation by Google searching. MATLAB 的文档非常丰富,您可以通过谷歌搜索了解每个操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.