使用goto语句Python化C ++块

问题描述 投票:3回答:3

我一直在Python化一个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);
        ...

他有一个名为“Jump”的标签,他去了。我们怎么能把这个Python化呢?因为我们已经处于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)
python c++ goto
3个回答
4
投票

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

(增量+ 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 ++代码中一样。


0
投票

由于所有跳转似乎都以i ++开头,而jump是循环的开始,所以看起来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)
    ...

我个人只是反转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)
        ...

0
投票

当然。我建议这个。

# 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)

由于我不知道其余的代码我不能准确。请告诉我是否发生任何错误。

© www.soinside.com 2019 - 2024. All rights reserved.