代码运行不下去了,怎么回事,请大家帮帮忙。
为什么我的代码运行到这里就运行不下去了
DWORD WINAPI CChatDlg::RecvProc(LPVOID lpParameter)
{
SOCKET socket = ((RECVPARAM*)lpParameter)->socket;
HWND hwnd = ((RECVPARAM*)lpParameter)->hwnd;
SOCKADDR_IN addrFrom;
int len = sizeof(SOCKADDR);
char recvBuf[200];
char tempBuf[300];
int retVal;
while (TRUE)
{
retVal = recvfrom(socket, recvBuf, 200, 0, (SOCKADDR*)&addrFrom, &len);//这里运行不下去了,不返回值,直接跳出
if (SOCKET_ERROR == retVal)
{
break;
}
sprintf(tempBuf, "%s说:%s", inet_ntoa(addrFrom.sin_addr), recvBuf);
::PostMessage(hwnd, WM_RECVDATA, 0, (LPARAM)tempBuf);
}
return 0;
}
我的程序可以运行了,因为不小心写错了一个字,改过来了
DWORD WINAPI CChatDlg::RecvProc(LPVOID lpParameter)
{
SOCKET socket = ((RECVPARAM*)lpParameter)->socket;
HWND hwnd = ((RECVPARAM*)lpParameter)->hwnd;
SOCKADDR_IN addrFrom;
int len = sizeof(SOCKADDR_IN);
char recvBuf[200];
char tempBuf[400]; // 增加缓冲区大小以容纳格式化后的字符串
int retVal;
while (TRUE)
{
retVal = recvfrom(socket, recvBuf, 200, 0, (SOCKADDR*)&addrFrom, &len);
if (retVal == SOCKET_ERROR)
{
break;
}
sprintf(tempBuf, "%s说:%s", inet_ntoa(addrFrom.sin_addr), recvBuf);
::PostMessage(hwnd, WM_RECVDATA, 0, (LPARAM)tempBuf);
}
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:#include <iostream>
#include <vector>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/calib3d.hpp>
#include <pcl/point_cloud.h>
#include <pcl/kdtree/kdtree_flann.h>
struct Point {
int x;
int y;
};
// Find regular points and special points with detected circle centers.
// regular_points: All the detected points including special points. Output will delete special points.
// special_points: All the special points.
void find_points(std::vector<Point> ®ular_points, std::vector<Point> &special_points) {
// tmp input point cloud
std::vector<Point> tmp_pt = regular_points;
// Cvt to Point cloud
pcl::PointCloud<pcl::PointXY>::Ptr cloud(new pcl::PointCloud<pcl::PointXY>);
cloud->width = tmp_pt.size();
cloud->height = 1;
int x_min = tmp_pt[0].x, y_min;
cloud->points.resize(cloud->width * cloud->height);
for (int i = 0; i < tmp_pt.size(); ++i) {
cloud->points[i].x = tmp_pt[i].x;
cloud->points[i].y = tmp_pt[i].y;
// find the point at the left bottom corner.
if (tmp_pt[i].x < x_min) {
x_min = tmp_pt[i].x;
y_min = tmp_pt[i].y;
}
}
pcl::PointXY minXY = pcl::PointXY(x_min, y_min);
// Build kdtree via point cloud
pcl::KdTreeFLANN<pcl::PointXY> kdTree;
kdTree.setInputCloud(cloud);
/*** K nearst searching method: roughly find the radius***/
int K = 2;
std::vector<int> pointIdxNKNSearch(K);
std::vector<float> pointNKNSquaredDistance(K);
double radius;
if (kdTree.nearestKSearch(minXY, K, pointIdxNKNSearch, pointNKNSquaredDistance) > 0)
radius = sqrt(pointNKNSquaredDistance.back()) / 2 * sqrt(2) + 5;
/*** Radius searching method ***/
// Set radius. The radius is chosen empirically.
std::vector<int> deletID;
for (int j = 0; j < cloud->size(); ++j) {
std::vector<int> radiusIndiceP;
std::vector<float> radiusSqrDistanceP;
// if found more than 3 points within the radius return true. 3 is chosen in consideration of the image.
if (kdTree.radiusSearch(cloud->points[j], radius, radiusIndiceP, radiusSqrDistanceP) &
(radiusIndiceP.size() > 3)) {
deletID.push_back(j);
// std::cout << "Special point index " << j << std::endl;
special_points.push_back(regular_points[j]);
}
}
for (std::vector<int>::reverse_iterator k = deletID.rbegin(); k != deletID.rend(); ++k)
regular_points.erase(regular_points.begin() + *k);
}
// Find circles in the image using Hough circle detection method.
// centers: the center coordinate of circles.
// img: source image.
void find_circles(std::vector<Point> ¢ers, cv::Mat &img) {
// Cvt img to gray
cv::Mat img_gray;
cv::cvtColor(img, img_gray, cv::COLOR_BGR2GRAY);
// Adapt threshold
cv::Mat img_threshold;
cv::threshold(img_gray, img_threshold, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
std::vector<cv::Vec3f> circles;
// Detect circles
cv::GaussianBlur(img_threshold, img_threshold, cv::Size(3, 3), 2, 2);
// cv::HoughCircles(img_threshold, circles, CV_HOUGH_GRADIENT, 1, img_threshold.cols / 40, 100, 5, 8, 15);
cv::HoughCircles(img_threshold, circles, CV_HOUGH_GRADIENT, 1, 10, 100, 10, 5, 13);
// The results will be better if optimizing centers on the basis of the current hough circles (Given ROI)
// But I didn't write the function. ^_^
// Save centers' coordinate
for (size_t i = 0; i < circles.size(); ++i) {
cv::Vec3f c = circles[i];
Point center;
center.x = c[0];
center.y = c[1];
centers.push_back(center);
}
std::cout << "Points: " << centers.size() << " in total." << std::endl;
}
// Visualization of points. Regular points are in green. Special points are in red.
// regular_points: All the detected points excluding special points.
// special_points: All the special points.
void draw_circles(std::vector<Point> ®ular_points, std::vector<Point> &special_point, cv::Mat img) {
// Draw regular points
std::cout << "Regular points: " << regular_points.size() << " in total." << std::endl;
for (size_t i = 0; i < regular_points.size(); ++i) {
Point center;
center = regular_points[i];
cv::circle(img, cv::Point(center.x, center.y), 3, cv::Scalar(0, 255, 0), 3, cv::LINE_AA);
}
// Draw special points
std::cout << "Special points: " << special_point.size() << " in total." << std::endl;
for (size_t j = 0; j < special_point.size(); ++j) {
Point center_ir;
center_ir = special_point[j];
cv::circle(img, cv::Point(center_ir.x, center_ir.y), 3, cv::Scalar(0, 0, 255), 3, cv::LINE_AA);
}
cv::imshow("Circle img", img);
cv::waitKey();
}
// Main function
// Assumption 1: The input image is pre-calibrated.
// Assumption 2: Points on the border or out of the border are neglected.
int main(int argc, char **argv) {
const char *filename = argc >= 2 ? argv[1] : "../data/image2.png";
cv::Mat img0 = cv::imread(filename, cv::IMREAD_COLOR);
if (img0.empty()) {
std::cout << "Failed in loading imag." << std::endl;
std::cout << "File dir is:" << filename << std::endl;
return 0;
}
std::vector<Point> centers, special_points;
find_circles(centers, img0); // Find circles
find_points(centers, special_points);
draw_circles(centers, special_points, img0);
return 1;
}
希望能找到心仪的工作😁