带有opencv的Sky imager项目,是否可以合并此代码?

问题描述

我有一个项目,该项目基本上每两分钟拍摄一次,并对它们进行分析以进行云检测和云覆盖。因此,今天我找到了同一位作者的代码,该作者为云检测进行了编码。但是我不知道如何将旧代码与新代码结合起来?

这是我的代码,这是我要运行的主要代码

import datetime
import os
from time import sleep
from picamera import PiCamera

file = 'latest.jpg'
basedir = '/home/pi/Pictures'
fisheye_radius = None  # The radius in pixels of the fisheye lens area,set to None if no fisheye lens

# open the camera and take the latest image
camera = PiCamera()
camera.start_preview()
# wait 2 seconds for the camera to initialise
sleep(2)
# capture and save an image to 'file'
camera.capture(file)

# capture the timestamp
Now = datetime.datetime.Now()
# create the relevant folders if they don't already exist
if not os.path.isdir(basedir + Now.strftime("%Y")):
    os.mkdir(basedir + Now.strftime("%Y"))
if not os.path.isdir(basedir + Now.strftime("%Y/%m")):
    os.mkdir(basedir + Now.strftime("%Y/%m"))
if not os.path.isdir(basedir + Now.strftime("%Y/%m/%d")):
    os.mkdir(basedir + Now.strftime("%Y/%m/%d"))

# move the new image to within its relevant folder with a timestamped filename
new_file = basedir + Now.strftime("%Y/%m/%d/%Y-%m-%d-%H-%M-%s.jpg")
os.rename(file,new_file)

# Only imported if needed
from makebinary import *
makebinary(new_file,fisheye_radius)

这是我正在使用的其他代码

# Cloud coverage computation for on-board computation
import cv2
import numpy as np
import os
from datetime import datetime

# User defined functions
from cmask import *

def makebinary(imagepath,radiusMask = None):
    startTime = datetime.Now()
    
    # Read the input image. Mention the complete path of the image
    im = cv2.imread(imagepath)
    
    
    # ---------- Resized -------------
    # Resize the image for faster processing
    resize_factor = 0.2
    im_re = cv2.resize(im,(0,0),fx=resize_factor,fy=resize_factor)
    index = [np.shape(im_re)[0]/2,np.shape(im_re)[1]/2]#[345,518]  # Center of the image
    if radiusMask:
        im_mask = cmask(index,resize_factor*radiusMask,im_re) # Mask with radius 200 pixels
    else:
        s = (np.shape(im_re)[0],np.shape(im_re)[1])
        im_mask = np.ones(s)
    # Extract the color channels
    B,G,R = cv2.split(im_re.astype(float))
    
    # Determine a good measure for detecting clouds vs sky
    BR_add = B + R
    BR_subtract = B - R
    # Handle X/0 and 0/0 errors,and remove NaNs (not a number)
    with np.errstate(divide='ignore',invalid='ignore'):
        BR_ratio = BR_subtract / BR_add
        BR_ratio = np.nan_to_num(BR_ratio)
    
    # normalize to 0-255 range and convert to 8-bit unsigned integers
    normalized = cv2.normalize(BR_ratio,None,255,cv2.norM_MINMAX).astype(np.uint8)
    
    # apply the mask to get only the pixels of interest
    camera_mask = im_mask.astype(bool)
    masked = normalized[camera_mask]
    
    # use Otsu's method to separate clouds and sky
    threshold,result = cv2.threshold(masked,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    
    # invert the result so clouds are white (255) and sky is black (0)
    inverted = cv2.bitwise_not(result)
    
    # determine the cloud coverage
    cloud_pixels = np.count_nonzero(inverted == 255)
    total_pixels = result.size
    cloud_coverage = cloud_pixels / total_pixels
    
    # create a mask of where the clouds are
    cloud_image_mask = np.zeros(camera_mask.shape,dtype=np.uint8)
    cloud_image_mask[camera_mask] = inverted.flatten()
    
    # reassign names (badly) to interface with the following code
    th_img,coverage = cloud_image_mask,cloud_coverage
    
    print ('Coverage is ',coverage)
    
    print (datetime.Now() - startTime)
    
    
    path_components = imagepath.split("/")
    full_image_name = path_components[-1]
    imagenameparts = full_image_name.split(".")
    image_name = imagenameparts[0]
    ext_name = imagenameparts[1]
    
    save_path = '/'.join(path_components[:-1]) + '/' + image_name + '-mask.' + ext_name
    
    cv2.imwrite(save_path,th_img)
    
    return(coverage)

这是我使用的最后一个代码

# This function generates the circular mask for the input fish-eye image

# Input:
# index = center co-ordanates of the image
# radius = radius of the circular image
# array = input image

# Output:
# image_mask = binary output image

import numpy as np

def cmask(index,radius,array):
    
  a,b = index
  is_rgb = len(array.shape)

  if is_rgb == 3:
     ash = array.shape
     nx=ash[0]
     ny=ash[1]

  else:
     nx,ny = array.shape
  
  s = (nx,ny)
  image_mask = np.zeros(s)
  y,x = np.ogrid[-a:nx-a,-b:ny-b]
  mask = x*x + y*y <= radius*radius
  image_mask[mask] = 1
  return(image_mask)

所以我想将这些代码与此代码结合起来

https://github.com/Soumyabrata/solar-irradiance-estimation

对于这个,我只想要“ Calculateluminance.py”和“ sun_positions_day_files.py”,我也想这个“ findSelectedFiles.py”,但是对于我来说,我希望所选的照片来自相机pi拍摄的照片

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)