我想计算数值标量场的 3D 体积积分。
在这篇文章中,我将使用一个可以精确计算积分的示例。因此我选择了以下函数:
在 Python 中,我定义了函数和 3D 中的一组点,然后生成这些点处的离散值:
import numpy as np
# Make data.
def function(x, y, z):
return x**y**z
N = 5
grid = np.meshgrid(
np.linspace(0, 1, N),
np.linspace(0, 1, N),
np.linspace(0, 1, N)
)
points = np.vstack(list(map(np.ravel, grid))).T
x = points[:, 0]
y = points[:, 1]
z = points[:, 2]
values = [function(points[i, 0], points[i, 1], points[i, 2])
for i in range(len(points))]
Run Code Online (Sandbox Code Playgroud)
如果我不知道底层函数,即如果我只有坐标 ( x, y, z) 和 ,我怎样才能找到积分values?
我正在尝试计算向量场的散度:
Fx = np.cos(xx + 2*yy)
Fy = np.sin(xx - 2*yy)
F = np.array([Fx, Fy])
Run Code Online (Sandbox Code Playgroud)
这是基于散度的解析计算的散度 (div(F) = dF/dx + dF/dy ) 的样子(请参阅此处的Wolfram Alpha ):
分歧:
div_analy = -np.sin(xx + 2*yy) - 2*np.cos(xx - 2*yy)
Run Code Online (Sandbox Code Playgroud)
编码:
# Number of points (NxN)
N = 50
# Boundaries
ymin = -2.; ymax = 2.
xmin = -2.; xmax = 2.
# Create Meshgrid
x = np.linspace(xmin,xmax, N) …Run Code Online (Sandbox Code Playgroud) 我有这个矩阵:
a = [1 2 2 1; 1 1 2 2]
% 1 2 2 1
% 1 1 2 2
Run Code Online (Sandbox Code Playgroud)
我想找到所有的1并将它们归零.
[~, a_i] = find(a == 1);
a(a_i) = 0
% 0 2 2 1
% 0 0 2 2
Run Code Online (Sandbox Code Playgroud)
为什么第一排还有1?
我有以下图片:
转换为base64,看起来是这样的:
import base64
filename = 'image.jpg'
with open(filename, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
image_file.close()
with open("encoded_string.txt", "w") as converted_file:
converted_file.write(str(encoded_string))
converted_file.close()
Run Code Online (Sandbox Code Playgroud)
在此处下载输出文件 (base64):https : //file.io/NXV7v4
现在,我的问题是:
如何检索转换后的图像并将其显示在 jupyter notebook 中,而无需存储它?
基于 [this][2] 问题,我尝试了:
from PIL import Image
import cv2
import io
# Take in base64 string and return cv image
def stringToRGB(base64_string):
imgdata = base64.b64decode(str(base64_string))
image = Image.open(io.BytesIO(imgdata))
return cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
stringToRGB(encoded_string)
Run Code Online (Sandbox Code Playgroud)
但我得到了:
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-43-2564770fa4af> in <module>()
----> 1 stringToRGB(encoded_string)
<ipython-input-42-538f457423e9> …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一种方法来转换字幕文件,以便每个字幕始终只有一个句子。
我的想法如下:
1.1->我得到字幕的持续时间
1.2->计算 characters_per_second
1.3->使用它来存储(里面dict_times_word_subtitle)说单词的时间i
我从全文中提取句子
对于每个句子:
3.1我在(内部dict_sentences_subtitle)存储用特定单词讲句子所花费的时间(从中我可以得到说出来的持续时间)
现在,我已经编写了以下代码:
#---------------------------------------------------------
import pysrt
import re
from datetime import datetime, date, time, timedelta
#---------------------------------------------------------
def convert_subtitle_one_sentence(file_name):
sub = pysrt.open(file_name)
### ----------------------------------------------------------------------
### Store Each Word and the Average Time it Takes to Say it in a dictionary
### ----------------------------------------------------------------------
dict_times_word_subtitle = {}
running_variable = 0
for i in range(len(sub)):
subtitle_text = sub[i].text
subtitle_duration = (datetime.combine(date.min, sub[i].duration.to_time()) - datetime.min).total_seconds()
# Compute …Run Code Online (Sandbox Code Playgroud) 你知道有一种算法可以看出图像上有笔迹吗?我没兴趣知道字迹写的是什么,只知道有一个礼物?
我有一段有人用手写填充幻灯片的视频。我的目标是确定幻灯片中已经填充了多少手写内容。
有问题的视频可以在这里下载:http : //www.filedropper.com/00_6
对于这个特定的视频,量化幻灯片中手写的内容已经提出了一个很好的解决方案
该解决方案基于将用于手写的特定颜色的数量相加。但是,如果笔迹不是蓝色而是在非笔迹上也可以找到的任何其他颜色,则此方法将不起作用。
因此,我很想知道,是否存在更通用的解决方案来确定图像上是否存在笔迹?
到目前为止我所做的: 我正在考虑提取图像的轮廓,然后根据轮廓的弯曲程度以某种方式检测手写部分(但我不知道如何做那部分)。不过,这可能不是最好的主意,因为它并不总是正确的......
import cv2
import matplotlib.pyplot as plt
img = cv2.imread(PATH TO IMAGE)
print("img shape=", img.shape)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("image", gray)
cv2.waitKey(1)
#### extract all contours
# Find Canny edges
edged = cv2.Canny(gray, 30, 200)
cv2.waitKey(0)
# Finding Contours
# Use a copy of the image e.g. edged.copy()
# since findContours alters the image
contours, hierarchy = cv2.findContours(edged,
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.imshow('Canny Edges After Contouring', edged) …Run Code Online (Sandbox Code Playgroud) 我想获取列表中值不为 None 的范围,例如:
test1 = [None, 0, None]
test2 = [2,1,None]
test3 = [None,None,3]
test4 = [1,0,None,0,0,None,None,1,None,0]
res1 = [[1,1]]
res2 = [[0,1]]
res3 = [[2,2]]
res4 = [[0,1],[3,4],[7,7],[9,9]]
Run Code Online (Sandbox Code Playgroud)
这是我超长的实现,它并不完美工作......
def get_not_None_ranges(list_):
# Example [0, 2, None, 1, 4] -> [[0, 1], [3, 4]]
r = []
end_i = len(list_)-1
if list_[0] == None:
s = None
else:
s = 0
for i, elem in enumerate(list_):
if s != None:
if elem == None and end_i != i: …Run Code Online (Sandbox Code Playgroud) 我有一个熊猫数据框:
import pandas as pd
import numpy as np
df = pd.DataFrame(columns=['Text','Selection_Values'])
df["Text"] = ["Hi", "this is", "just", "a", "single", "sentence.", "This", np.nan, "is another one.","This is", "a", "third", "sentence","."]
df["Selection_Values"] = [0,0,0,0,0,1,0,0,1,0,0,0,0,0]
print(df)
Run Code Online (Sandbox Code Playgroud)
输出:
Text Selection_Values
0 Hi 0
1 this is 0
2 just 0
3 a 0
4 single 0
5 sentence. 1
6 This 0
7 NaN 0
8 is another one. 1
9 This is 0
10 a 0
11 third 0
12 sentence 0 …Run Code Online (Sandbox Code Playgroud) 我最近偶然发现了一个问题:在jupyter笔记本中对图像进行交互式标记, 这很有趣。
由于对python编程经验很少,我尝试使用jupyter笔记本从答案中运行提供的代码,但是我无法以某种方式使其工作。我相信导入图像时做错了什么。我正在尝试从位于“ PATH”中的名为“ images”的文件夹中导入所有图像。
这是完整的代码:
import cv2
import os
import ipywidgets as widgets
import functools
images_list = []
os.chdir(PATH)
# Load in the images
for filepath in os.listdir('images/'):
images_list.append(cv2.imread('images/{0}'.format(filepath),0))
COLS = 4
ROWS = 2
IMAGES = images_list
IMG_WIDTH = 200
IMG_HEIGHT = 200
def on_click(index):
print('Image %d clicked' % index)
rows = []
for row in range(ROWS):
cols = []
for col in range(COLS):
index = row * COLS + col
image = widgets.Image(
value=IMAGES[index], width=IMG_WIDTH, height=IMG_HEIGHT …Run Code Online (Sandbox Code Playgroud) 我想使用 python 从这些数据中检测峰值:
data = [1.0, 0.35671858559485703, 0.44709399319470694, 0.29438948200831194, 0.5163825635166547, 0.3036363865322419, 0.34031782308777747, 0.2869558046065574, 0.28190537831716, 0.2807516154537239, 0.34320479518313507, 0.21117275536958913, 0.30304626765388043, 0.4972542099530442, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.18200891715227194, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, …Run Code Online (Sandbox Code Playgroud) python ×9
numpy ×3
list ×2
matplotlib ×2
opencv ×2
python-3.x ×2
base64 ×1
integral ×1
interactive ×1
math ×1
matlab ×1
matrix ×1
pandas ×1
regex ×1
regex-greedy ×1
string ×1
subtitle ×1