Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.

8 views (last 30 days)
import numpy as np
from CV2 import CV2
Image = CV2.Imread('Image.jpg');
Image = CV2.Imresize(Image, 0.5);
Image_gray = CV2.cutcolor(image, CV2.COLOR_BGR2GRAY);
CV2.Imshow("binary", image_gray)
histg = CV2.calcHist([Image_gray], [0], None, [255], [0, 255]);
within = [];
For i in range (len(histg)):
x,y = np.split(histg, [1i]);
x1 = np.sum(x)/(image.shape[0] * image.shape[1]);
y1 = np.sum(y)/(image.shape[0] * image.shape[1]);
x2 = np.sum([j*t for j,t in enumerate(x)])/np.sum(x);
y2 = np.sum([j*t for j,t in enumerate(y)])/np.sum(y);
x3 = np.sum([[j-x2]* 2xt for j,t in enumerate(y)]);
within.append(x1*x3+y1*y3)
m = np.argmin(within);
(thresh, Bin) = (v2.threshold(image_gray,m;255;CV2+threshold_OSTU))
(V2.imshow("binary", Bin))
(V2.waitley(0));
>> Untitled4
File: Untitled4.m Line: 11 Column: 28
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
Please help me fix this code/error

Answers (1)

Walter Roberson
Walter Roberson on 21 Apr 2021
import numpy as np
MATLAB does have an import statement, but it only accepts a single name after it; it does not support using as
from CV2 import CV2
That would be equivalent to calling
from('CV2', 'import', 'CV2')
which would try to find a function named from on the MATLAB path. By default, from is most likely to end up referring to a method of java.util.Date and would fail when you tried to pass character vectors to it.
For i in range (len(histg)):
MATLAB is case-sensitive about the keywords. For is not a valid keyword in MATLAB: you would need to use for
MATLAB does not have any syntax
for VARIABLE in EXPRESSION
for loops must be of the form
for VARIABLE = EXPRESSION
range does exist in MATLAB as part of the Statistics and Machine Learning Toolbox, but it returns maximum of the expression minus the minimum of the expression.
MATLAB does not provide any len function. And remember that whatever len() is, it would have to return multiple values in order for the Statistics function range to operate on it.
Also notice the colon you have at the end of for . In MATLAB, colon is a range operator, and must have values on both sides of it.
... and other problems as well.

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!