def RandomWalkMetHastings(img, AS, fMap, sigma, N): imH, imW = img.shape #Split array to quickly go through pixels pUsed = utils.nonNan(AS) values, points = utils.seperateArray(AS, pUsed) for i in range(values.size): y, x = points[i] yPrev = y = int(y) xPrev = x = int(x) for j in range(N): xProp = utils.walkIndex(xPrev, imW - 1, sigma) yProp = utils.walkIndex(yPrev, imH - 1, sigma) # Ratio of new point compared to previous on feature map α = min((fMap[yProp][xProp]) / (fMap[yPrev][xPrev]), 1) # Random int between 1 and 0 r = random.uniform(0, 1) # Check proposal if r < α: # Check if point is used if math.isnan(AS[yProp][xProp]): yPrev = yProp xPrev = xProp AS[y][x] = np.nan AS[yPrev][xPrev] = img[yPrev][xPrev] nearestAS = utils.nInterp2D(pUsed, AS) #nearestAS = AS return nearestAS
def MetHastings(img, pixels, fMap, N): imH, imW = img.shape #Define AS value array and Feature Map #fMap = np.zeros((imH, imW)) AS = np.empty((imH, imW)) AS[:] = np.nan #Pixels sampled pCount = 0 #Set initial Met Hastings position rX = random.randint(0, imW - 1) rY = random.randint(0, imH - 1) nX = rX nY = rY n = img[rY][rX] AS[rY][rX] = n pCount += 1 #Determine if the iteration succeeded in finding a good candidate accept = False #Loop through other pixels while pCount < pixels: accept = False for i in range(N): # Random x and y Values rX = random.randint(0, imW - 1) rY = random.randint(0, imH - 1) # Ratio of new point compared to previous on feature map α = min((fMap[rY][rX]) / (fMap[nY][nX]), 1) # Random int between 1 and 0 r = random.uniform(0, 1) if r < α: # Check if pixel is used if math.isnan(AS[rY][rX]): nX = rX nY = rY n = img[rY][rX] accept = True if accept: # Check if pixel is used if math.isnan(AS[nY][nX]): AS[nY][nX] = n pCount += 1 nearestAS = utils.nInterp2D(pixels, AS) #nearestAS = AS return nearestAS
def uniformS(img, nPixels): imH, imW = img.shape if(nPixels > imH * imW): nPixels = imH * imW US = np.empty((imH, imW)) US[:] = np.nan US = utils.uniformSpread(img, nPixels, US) uniformNearest = utils.nInterp2D(nPixels, US) return uniformNearest
def randomAS(img, ROI, pixels, roiPort): imH, imW = img.shape AS = np.empty((imH, imW)) AS[:] = np.nan if pixels > imH * imW: print("Error: Pixel allocation is too large") pixels = imH * imW # Round ROI pixels newROIP = round(pixels * roiPort) totROI = 0 #Total ROI pixels for r in ROI: x, y, w, h = r totROI += w * h if newROIP > totROI: newROIP = totROI pCount = 0 for r in ROI: x, y, w, h = r #Portion of total ROI ROIPort = (w * h) / (totROI) nPixels = newROIP * ROIPort while pCount < nPixels: rX = random.randint(x, x + w - 1) rY = random.randint(y, y + h - 1) if math.isnan(AS[rY][rX]): AS[rY][rX] = img[rY][rX] pCount += 1 while pCount < pixels: rX = random.randint(0, imW - 1) rY = random.randint(0, imH - 1) if math.isnan(AS[rY][rX]): AS[rY][rX] = img[rY][rX] pCount += 1 nearestAS = utils.nInterp2D(pixels, AS) return nearestAS
def uniformAS(img, ROI, nPixels, rPort): imH, imW = img.shape if(nPixels > imH * imW): nPixels = imH * imW AS = np.empty((imH, imW)) AS[:] = np.nan #Can be an input bPort = 1 - rPort rPixels = round(nPixels * rPort) bPixels = round(nPixels * bPort) #Pixels Remaining pRem = 0 #Total pixels in ROI roiSum = 0 for r in ROI: x, y, w, h = r roiSum += ((w + 1) * (h + 1)) for r in ROI: x, y, w, h = r roiPort = ((w + 1) * (h + 1))/roiSum roiPixels = round(roiPort * rPixels) #Add on the remainding pixels roiPixels += pRem AS[y:y+h, x:x+w] = utils.uniformSpread(img[y:y+h, x:x+w], roiPixels, AS[y:y+h, x:x+w]) pRem = roiPixels - utils.nonNan(AS[y:y+h, x:x+w]) bPixels += pRem AS = utils.uniformSpread(img, bPixels, AS) pUsed = utils.nonNan(AS) uniformNearest = utils.nInterp2D(pUsed, AS) #uniformNearest = AS return uniformNearest
def randomS(img, pixels): imH, imW = img.shape RS = np.empty((imH, imW)) RS[:] = np.nan if pixels > imH * imW: print("Error: Pixel allocation is too large") pixels = imH * imW pCount = 0 while pCount < pixels: rX = random.randint(0, imW - 1) rY = random.randint(0, imH - 1) if math.isnan(RS[rY][rX]): RS[rY][rX] = img[rY][rX] pCount += 1 nearestRS = utils.nInterp2D(pixels, RS) return nearestRS