Pythonizing C++;带goto语句的块 我一直在对C++代码进行修改,我遇到了一个我陷入困境的情况。程序员使用goto语句来实现他的逻辑,由于Python中没有goto语句,而且我不想深入研究它有趣的goto实现,我想知道我们是否可以以某种方式将以下代码块pythonization: // 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); ... //在输入图像的所有检测到的圆上循环 对于(int j=0;j=0,ROIORC++ C++(C++)代码需要重新修改:C++代码需要修改: Goto < /Cord>语句和索引增加相当于一个继续指令(在C/C++程序中,几乎不需要 Goto ,绝对不在这里,原来的C++代码是“仿真”的。继续因为作者不知道它的存在):

Pythonizing C++;带goto语句的块 我一直在对C++代码进行修改,我遇到了一个我陷入困境的情况。程序员使用goto语句来实现他的逻辑,由于Python中没有goto语句,而且我不想深入研究它有趣的goto实现,我想知道我们是否可以以某种方式将以下代码块pythonization: // 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); ... //在输入图像的所有检测到的圆上循环 对于(int j=0;j=0,ROIORC++ C++(C++)代码需要重新修改:C++代码需要修改: Goto < /Cord>语句和索引增加相当于一个继续指令(在C/C++程序中,几乎不需要 Goto ,绝对不在这里,原来的C++代码是“仿真”的。继续因为作者不知道它的存在):,python,c++,goto,Python,C++,Goto,他有一个叫做“跳跃”的标签。我们怎么能把这个标签变成Pythonize呢?因为我们已经处于for循环中,所以我有点犹豫是否要引入更多的循环 提前谢谢 编辑:多亏了贡献者,我们提出了以下建议,但这陷入了一个无限循环: # Loop over all detected circles of the input image for j in range(len(center_vec)): while True: # arrange the ROI

他有一个叫做“跳跃”的标签。我们怎么能把这个标签变成Pythonize呢?因为我们已经处于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)
#在输入图像的所有检测圆上循环
对于范围内的j(len(center_vec)):
尽管如此:
#安排投资回报率
#加载当前中心点
center_now=center_vec[j]#以ROI的中心作为圆心
roi_height_width=int(圆形(圆形半径_vec[j]))#将半径作为roi的高度和宽度
roi\u高度\u宽度=int(圆形(圆形\u半径\u向量[j]))
roi\u角点\u x=int(圆形(现在为中心[0]-roi\u高度\u宽度/2))
roi\u角点\u y=int(圆形(现在为中心[1]-roi\u高度\u宽度/2))
#如果ROI在图像外,则跳过圆圈
如果roi_角_x<0:
j+=1
持续
如果roi_角_y<0:
j+=1
持续
如果roi\u角\u x+roi\u高度\u宽度>图像形状[1]:
j+=1
持续
如果roi\u角\u y+roi\u高度\u宽度>图像形状[0]:
j+=1
持续
#从输入图像矩形创建ROI
roi=img[roi\u corner\u y:roi\u corner\u y+roi\u high\u width,roi\u corner\u x:roi\u corner\u x+roi\u high\u width]
#创建ROI的HSV表示
hsv=cv2.CVT颜色(roi,cv2.COLOR\U BGR2HSV)

因为所有的跳转似乎都是由j++执行的,跳转是循环的开始,所以Python continue语句似乎可以很容易地解决您的问题

# 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)
    ...
#在输入图像的所有检测圆上循环
对于范围内(1,圆半径向量大小()):
#投资回报率的变量
roi\u高度\u宽度=0
roi_角_x=0
roi_角_y=0
现在点中心(roi\u高度\u宽度/2,roi\u高度\u宽度/2)
#加载aktuellen中心点
现在中心=中心向量[j]
#计算投资回报率
roi\高度\宽度=圆\半径\向量[j]
roi\u角\u x=现在居中\u.x-roi\u高度\u宽度/2
roi\u角落\u y=现在居中。y-roi\u高度\u宽度/2
#如果ROI在图像外,则跳过圆圈
如果roi\u corner\u x<0或roi\u corner\u y<0或roi\u corner\u x+roi\u height\u width>input\u img.cols或roi\u corner\u y+roi\u height\u width>input\u img.rows:
持续
#从输入图像创建ROI
Rect roi=Rect(roi\u角点x、roi\u角点y、roi\u高度\u宽度、roi\u高度\u宽度)
img\U roi=输入\U img(roi)
#创建ROI的HSV表示
单纯疱疹病毒()
CVT颜色(img_roi、hsv、彩色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)
        ...
#在输入图像的所有检测圆上循环
对于范围内(1,圆半径向量大小()):
#投资回报率的变量
roi\u高度\u宽度=0
roi_角_x=0
roi_角_y=0
现在点中心(roi\u高度\u宽度/2,roi\u高度\u宽度/2)
#加载aktuellen中心点
现在中心=中心向量[j]
#计算投资回报率
roi\高度\宽度=圆\半径\向量[j]
roi\u角\u x=现在居中\u.x-roi\u高度\u宽度/2
roi\u角落\u y=现在居中。y-roi\u高度\u宽度/2
#如果ROI在图像外,则跳过圆圈

如果Roier-CordNyx>=0,ROIORC++ C++(C++)代码需要重新修改:C++代码需要修改:<代码> Goto < /Cord>语句和索引增加相当于一个<代码>继续<代码>指令(在C/C++程序中,几乎不需要<代码> Goto ,绝对不在这里,原来的C++代码是“仿真”的。
继续
因为作者不知道它的存在):

或者只是在元素上迭代(更像python,因为它不使用索引):

在这两种情况下,<<代码> <代码>循环控制迭代。您只需告诉Python跳过下一次迭代,使用<代码>继续<代码>,就像在“新”C++代码中一样。

当然。 我建议这样做

#在输入图像的所有检测到的圆上循环。
对于范围内的j(len(圆半径向量)):
#投资回报率的变量。
roi\u高度\u宽度=0
roi_角_x=0
roi_角_y=0
#计算投资回报率
roi\高度\宽度=圆\半径\向量[j]
roi_角_x=c
for (int j = 0; j < circle_radius_vec.size(); ++j)
    {   
        ..
        // If ROI is outside of image skip circle
        if(roi_corner_x < 0){continue;}
for index in range(size):        
    ...
    if some_condition:
        continue
for a in the_list:
    # a is the current element of the list, not the index
    ...
    if some_condition:
        continue