簡體   English   中英

無法編寫接受 3 個參數並返回矩形坐標 (x,y,h,w) 的函數

[英]unable to write a function that take 3 arguments and return the rectangle coordinate (x,y,h,w)

我正在嘗試編寫一個帶有以下參數的函數:

def get_square(n, size, ebt):

    '''
      n is the number of square
      size is the size of the rectangle, in our case size=w=h
      ebt stand for elevation body temperature, it will a constant value
    '''

    return 

正方形的第一個坐標就是這樣計算的。 注意大小 = h = w

在此處輸入圖片說明

如下圖所示,隨着 n 的增加,我們應該有更多的正方形(網格狀形狀),該函數應該在如下所示的 txt 文件中返回每個正方形的 x、y、w、h(如果 n = 3):

Prediction_Region = x1、y1、w1、h1、x2、y2、w2、h2、x3、y3、w3、h3

Max_Temperture = ebt, ebt, ebt

我嘗試了多次實施,但似乎我錯過了一些東西。

這是一個代碼示例,我試圖從終端調用這個函數

import os, sys
import re
import math
import argparse
import numpy as np

FILE_NAME = 'output.txt'

def square(n, size, ebt):
    xs = 320
    ys = 256
    nx = int(xs)/2 - size/2
    ny = int(ys)/2 - size/2

    # the main thing should be here, but I am not able to implement it due to few error

    return 

# this is to write the output of square function in a text file.
def writetoendofline(lines, line_no, append_txt):
    lines[line_no] = lines[line_no].replace('\n', '') + append_txt + '\n'

# open the file
with open(FILE_NAME, 'r') as txtfile:
    lines = txtfile.readlines()
# this function take the line number and append a value at the end of it
# ultimately the output of square function should be entered here
writetoendofline(lines, 1, '23 ')
# writetoendofline(lines, 0, nx)
# write to the file
with open(FILE_NAME, 'w') as txtfile:
    txtfile.writelines(lines)
# write to the file
txtfile.close()


if __name__ == '__main__':
    n = sys.argv[1]
    size = sys.argv[2]
    ebt = sys.argv[3]
    square(n, size, ebt)

在此處輸入圖片說明

當屏幕尺寸為(width, height)且居中網格包含nxn矩形時,最左上角矩形的坐標為

x0 = (width - w * n) / 2
y0 = (height - h * n) / 2

要獲得所有矩形坐標,只需為每個新矩形將它們移動 w, h

def square(width, height, n, w, h):
    x0 = (width - n * w) // 2
    y0 = (height - n * h) //2
    coords = [[x0 + ix * w, y0 + iy * h] for iy in range(n) for ix in range(n)]
    return coords

print(square(320, 240, 3, 32, 24))

[[112, 84], [144, 84], [176, 84], 
 [112, 108], [144, 108], [176, 108], 
 [112, 132], [144, 132], [176, 132]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM