我一直在编写一个C++代码,我遇到了一种被困在其中的情况。程序员使用goto语句来实现他的逻辑,而且由于Python中没有goto语句,而且由于我不想钻研它的有趣的goto实现,所以我想知道我们是否可以以某种方式对以下块进行Python化:
// Loop over all detected circles of the input image
for (int j = 0; j < circle_radius_vec.size(); ++j)
{
Jump:
// Variables for ROI
int roi_height_width = 0;
int roi_corner_x = 0;
int roi_corner_y = 0;
Point center_now(roi_height_width/2, roi_height_width/2);
// Load aktuellen center point
center_now = center_vec[j];
// Calculate ROI
roi_height_width = circle_radius_vec[j];
roi_corner_x = center_now.x - roi_height_width/2;
roi_corner_y = center_now.y - roi_height_width/2;
// If ROI is outside of image skip circle
if(roi_corner_x < 0){j++; goto Jump;}
if(roi_corner_y < 0){j++; goto Jump;}
if((roi_corner_x+roi_height_width) > input_img.cols){j++; goto Jump;}
if((roi_corner_y+roi_height_width) > input_img.rows){j++; goto Jump;}
// Create ROI from input image
Rect roi = Rect(roi_corner_x, roi_corner_y, roi_height_width, roi_height_width);
Mat img_roi = input_img(roi);
// Create HSV representation of ROI
Mat hsv;
cvtColor(img_roi, hsv, COLOR_BGR2HSV);
...他有一个叫做“跳跃”的标签。我们怎么能把这幅画化为灰烬?由于我们已经处于for循环中,所以我有点不愿意引入更多的循环。
提前谢谢。
编辑:--感谢贡献者,提出了以下建议,但是这会陷入一个无限循环中:
# Loop over all detected circles of the input image
for j in range(len(center_vec)):
while True:
# arrange the ROI
# load the current center point
center_now = center_vec[j] # take the center of ROI as the center of circle
roi_height_width = int(round(circle_radius_vec[j])) # take the radius as height and width of the ROI
roi_height_width = int(round(circle_radius_vec[j]))
roi_corner_x = int(round(center_now[0] - roi_height_width / 2))
roi_corner_y = int(round(center_now[1] - roi_height_width / 2))
# If ROI is outside of image skip circle
if roi_corner_x < 0:
j += 1
continue
if roi_corner_y < 0:
j += 1
continue
if roi_corner_x + roi_height_width > img.shape[1]:
j += 1
continue
if roi_corner_y + roi_height_width > img.shape[0]:
j += 1
continue
# create ROI from input image Rect
roi = img[roi_corner_y:roi_corner_y+roi_height_width, roi_corner_x:roi_corner_x+roi_height_width]
# Create HSV representation of ROI
hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)发布于 2018-08-29 15:54:24
C++代码需要重做:goto语句和索引的增加相当于continue指令(在C/C++程序中几乎不需要goto,这里绝对不需要原始的C++代码,因为作者不知道它的存在,所以它“模拟”了continue ):
for (int j = 0; j < circle_radius_vec.size(); ++j)
{
..
// If ROI is outside of image skip circle
if(roi_corner_x < 0){continue;}注意,现在不需要手动增加索引,因为continue跳到下一个迭代,调用循环的++j。
( incrementation+goto的另一个问题是,如果特殊情况发生在数组的末尾,那么您可以在数组的边界之外读取:未定义的行为)
现在,您可以在python中直接转置:您有两个选项:
要么使用索引(就像C++代码那样)
for index in range(size):
...
if some_condition:
continue或者只是在元素上迭代(更多的pythonic元素,因为它不使用索引):
for a in the_list:
# a is the current element of the list, not the index
...
if some_condition:
continue在这两种情况下,for循环控制迭代。您只需告诉python跳过使用continue的下一次迭代,就像在“新”C++代码中一样。
发布于 2018-08-29 15:52:33
由于所有跳转似乎都是由j++预先执行的,而跳转是循环的开始,所以似乎Python语句可以很容易地解决您的问题?
# Loop over all detected circles of the input image
for in range(1, circle_radius_vec.size()):
# Variables for ROI
roi_height_width = 0
roi_corner_x = 0
roi_corner_y = 0
Point center_now(roi_height_width/2, roi_height_width/2)
# Load aktuellen center point
center_now = center_vec[j]
# Calculate ROI
roi_height_width = circle_radius_vec[j]
roi_corner_x = center_now.x - roi_height_width/2
roi_corner_y = center_now.y - roi_height_width/2
# If ROI is outside of image skip circle
if roi_corner_x < 0 or roi_corner_y < 0 or roi_corner_x + roi_height_width > input_img.cols or roi_corner_y + roi_height_width > input_img.rows:
continue
# Create ROI from input image
Rect roi = Rect(roi_corner_x, roi_corner_y, roi_height_width, roi_height_width)
img_roi = input_img(roi)
# Create HSV representation of ROI
hsv()
cvtColor(img_roi, hsv, COLOR_BGR2HSV)
...我个人只需要反转if语句并有条件地执行代码的下半部分,如下所示:
# Loop over all detected circles of the input image
for in range(1, circle_radius_vec.size()):
# Variables for ROI
roi_height_width = 0
roi_corner_x = 0
roi_corner_y = 0
Point center_now(roi_height_width/2, roi_height_width/2)
# Load aktuellen center point
center_now = center_vec[j]
# Calculate ROI
roi_height_width = circle_radius_vec[j]
roi_corner_x = center_now.x - roi_height_width/2
roi_corner_y = center_now.y - roi_height_width/2
# If ROI is outside of image skip circle
if roi_corner_x >= 0 and roi_corner_y >= 0 and roi_corner_x + roi_height_width <= input_img.cols and roi_corner_y + roi_height_width <= input_img.rows:
# Create ROI from input image
Rect roi = Rect(roi_corner_x, roi_corner_y, roi_height_width, roi_height_width)
img_roi = input_img(roi)
# Create HSV representation of ROI
hsv()
cvtColor(img_roi, hsv, COLOR_BGR2HSV)
...发布于 2018-08-29 16:06:17
好的。我建议你这么做。
# Loop over all detected circles ot the input image.
for j in range(len(circle_radius_vec)):
# Variables for ROI.
roi_height_width = 0
roi_corner_x = 0
roi_corner_y = 0
# Calculate ROI
roi_height_width = circle_radius_vec[j]
roi_corner_x = center_now.x - roi_height_width/2
roi_corner_y = center_now.y - roi_height_width/2
center_now = Point(roi_height_width/2, roi_height_width/2)
# Load aktuellen center point.
center_now = center_vec[j]
# Calculate ROI.
if roi_corner_x < 0:
continue
if roi_corner_y < 0:
continue
if roi_corner_x + roi_height_width > input_img.cols:
continue
# Create ROI from input image.
roi = Rect(roi_corner_x, roi_corner_y, roi_height_width, roi_height_width)
img_roi = input_img(roi)
# Create HSV representation of ROI.
hsv = None
cvtColor(img_roi, hsv, COLOR_BGR2HSV)因为我不知道剩下的代码,所以我不能说得很准确。请告诉我是否有任何错误发生。
https://stackoverflow.com/questions/52081341
复制相似问题