for (int i = 0; i < score_table.size(); i++) { if (score_table[i] > detection_threshold_) { cv::Point2f pos = feature_table[i]; features.push_back(cv::Point2f(pos.x, pos.y)); } } zero_occupancy_grid();
时间: 2024-02-14 13:35:41 浏览: 74
这段代码是在遍历之前更新过的 `score_table` 分数表,将分数高于指定阈值 `detection_threshold_` 的网格对应的特征点坐标存储到 `features` 向量中。对于每个分数高于阈值的网格,获取其对应的特征点坐标,并将其存储到 `features` 向量中。最后,调用 `zero_occupancy_grid()` 函数将占用网格清零,以便下一次特征点检测时再次使用。
相关问题
#include <stdio.h> #include <stdlib.h> #define ROW 6 #define COL 6 int main() { int score[ROW][COL]; int cards[6]; int i, j, k, num, dir, total_score; // input score table for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { scanf("%d", &score[i][j]); } } // input cards for (i = 0; i < 6; i++) { scanf("%d", &cards[i]); } // input and process each customer for (k = 0; k < 3; k++) { total_score = 0; scanf("%d %d", &num, &dir); // calculate score if (cards[num-1] == 0) { printf("Invalid card number!\n"); continue; } if (dir == 1) { for (i = 0; i < COL; i++) { total_score += score[num-1][i]; } } else if (dir == 2) { for (i = 0; i < ROW; i++) { total_score += score[i][num-1]; } } else { printf("Invalid direction!\n"); continue; } printf("%d\n", total_score); } return 0; } 改进上述代码
上述代码已经可以正常运行,但是可以进行一些改进:
1. 可以增加注释,让代码更易读懂。
2. 可以在输入时对输入数据进行合法性检查,比如积分表中的数字应该在1~100之间,卡牌数字应该在0~6之间,等等。
3. 可以将代码拆分成多个函数,让主函数更加简洁明了。
4. 可以增加错误处理机制,比如输入数据格式错误时给出提示,让程序更加健壮。
下面是改进后的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#define ROW 6
#define COL 6
// 检查输入的数字是否合法,若不合法则返回0,否则返回1
int isValidNumber(int num, int min, int max)
{
if (num < min || num > max)
{
return 0;
}
return 1;
}
// 读取积分表
void readScoreTable(int score[ROW][COL])
{
int i, j;
printf("Please input the score table:\n");
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
scanf("%d", &score[i][j]);
if (!isValidNumber(score[i][j], 1, 100))
{
printf("Invalid input: score should be between 1 and 100!\n");
exit(1);
}
}
}
}
// 读取卡牌
void readCards(int cards[6])
{
int i;
printf("Please input the cards:\n");
for (i = 0; i < 6; i++)
{
scanf("%d", &cards[i]);
if (!isValidNumber(cards[i], 0, 6))
{
printf("Invalid input: card number should be between 0 and 6!\n");
exit(1);
}
}
}
// 计算得分
int calculateScore(int score[ROW][COL], int cards[6], int num, int dir)
{
int i, total_score = 0;
if (cards[num-1] == 0)
{
printf("Invalid input: card number %d is hidden!\n", num);
return 0;
}
if (dir == 1)
{
for (i = 0; i < COL; i++)
{
total_score += score[num-1][i];
}
}
else if (dir == 2)
{
for (i = 0; i < ROW; i++)
{
total_score += score[i][num-1];
}
}
else
{
printf("Invalid input: direction should be 1 or 2!\n");
return 0;
}
return total_score;
}
// 处理每个顾客的输入和输出
void processCustomer(int score[ROW][COL], int cards[6])
{
int i, num, dir, total_score;
for (i = 0; i < 3; i++)
{
printf("Please input the card number and direction for customer %d:\n", i+1);
scanf("%d %d", &num, &dir);
total_score = calculateScore(score, cards, num, dir);
printf("Customer %d's score is %d\n", i+1, total_score);
}
}
int main()
{
int score[ROW][COL];
int cards[6];
readScoreTable(score);
readCards(cards);
processCustomer(score, cards);
return 0;
}
```
import requests from bs4 import BeautifulSoup import matplotlib.pyplot as plt import pandas as pd PLAYERS_LIMIT = 25 TABLE_CLASS_NAME = "players_table" plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False def get_top_players_scores(limit=PLAYERS_LIMIT, table_class_name=TABLE_CLASS_NAME): url = "https://nba.hupu.com/stats/players" response = requests.get(url) soup = BeautifulSoup(response.text, "html.parser") players = [] scores = [] table = soup.find("table", class_=table_class_name) rows = table.find_all("tr") for row in rows[1:limit+1]: cols = row.find_all("td") player = cols[1].text.strip() score_range = cols[4].text.strip() score_parts = score_range.split("-") min_score = float(score_parts[0]) max_score = float(score_parts[1]) score = int((min_score + max_score) / 2) players.append(player) scores.append(score) return players, scores def plot_top_players_scores(players, scores): data = {"Player": players, "Score": scores} df = pd.DataFrame(data) fig, ax = plt.subplots(figsize=(12, 6)) ax.bar(players, scores, color='green', alpha=0.6) ax.set_xlabel('球员', fontsize=12) ax.set_ylabel('得分', fontsize=12) ax.set_title('NBA球员得分', fontsize=14) plt.xticks(rotation=45, ha='right', fontsize=8) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) for i, score in enumerate(scores): ax.text(i, score+0.5, str(score), ha='center', va='bottom') writer = pd.ExcelWriter('plot_top_players_scores.xlsx') df.to_excel(writer, index=False) writer.save() fig.tight_layout() plt.show() if __name__ == "__main__": players, scores = get_top_players_scores() plot_top_players_scores(players, scores)这段代码生成的excel损坏
这可能是因为在代码中将Excel文件的写操作放在了`for`循环中,每次迭代都会打开并写入Excel文件,导致Excel文件损坏。应该将Excel文件的写操作放在循环外面,只在循环结束后执行一次即可。修改后的代码如下:
```python
import requests
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
import pandas as pd
PLAYERS_LIMIT = 25
TABLE_CLASS_NAME = "players_table"
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
def get_top_players_scores(limit=PLAYERS_LIMIT, table_class_name=TABLE_CLASS_NAME):
url = "https://nba.hupu.com/stats/players"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
players = []
scores = []
table = soup.find("table", class_=table_class_name)
rows = table.find_all("tr")
for row in rows[1:limit+1]:
cols = row.find_all("td")
player = cols[1].text.strip()
score_range = cols[4].text.strip()
score_parts = score_range.split("-")
min_score = float(score_parts[0])
max_score = float(score_parts[1])
score = int((min_score + max_score) / 2)
players.append(player)
scores.append(score)
return players, scores
def plot_top_players_scores(players, scores):
data = {"Player": players, "Score": scores}
df = pd.DataFrame(data)
fig, ax = plt.subplots(figsize=(12, 6))
ax.bar(players, scores, color='green', alpha=0.6)
ax.set_xlabel('球员', fontsize=12)
ax.set_ylabel('得分', fontsize=12)
ax.set_title('NBA球员得分', fontsize=14)
plt.xticks(rotation=45, ha='right', fontsize=8)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
for i, score in enumerate(scores):
ax.text(i, score+0.5, str(score), ha='center', va='bottom')
writer = pd.ExcelWriter('plot_top_players_scores.xlsx')
df.to_excel(writer, index=False)
writer.save()
fig.tight_layout()
plt.show()
if __name__ == "__main__":
players, scores = get_top_players_scores()
plot_top_players_scores(players, scores)
```
这样修改后,Excel文件的写操作只会在循环结束后执行一次,不会出现损坏的问题。
阅读全文