現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
array values are overwritten
1 回表示 (過去 30 日間)
古いコメントを表示
Hello,
I am working with for loop in my code. I am getting the answer but after 8 steps i.e. from the step 9, the value of array is overwritten.
means The value I was getting at column 2 in the first 8 steps , I am getting that value in the column 5 when i start with step 9.
please help me for the same.
16 件のコメント
KALYAN ACHARJYA
2019 年 7 月 19 日
編集済み: KALYAN ACHARJYA
2019 年 7 月 19 日
Please share the code, if possible?
swati mane
2019 年 7 月 19 日
編集済み: Jan
2019 年 7 月 19 日
imgFiles=dir('*.jpg');
numFiles=length(imgFiles);
valueOfA=zeros(1,numFiles);
for k=1:1:numFiles
n=k+1;
if n<=numFiles
img=imread(imgFiles(k).name);
compareimg=imread(imgFiles(n).name);
P=imabsdiff(img,compareimg);
figure
imshow(P,[]);
N=nnz(P);
valueOfA(k)=N;
else
end
end
This is my code. When I read 8 images I am getting right answers. But when I work with images more than 8 the array values are overwritten.
means The value I was getting at column 2 in the first 8 images , I am getting that value in the column 5 when i start with image 9.
madhan ravi
2019 年 7 月 19 日
Swati learn to format your code properly by selecting the code and pressing the code button.
Jan
2019 年 7 月 19 日
@swati mane: Please post explicitly, which variables are affected. Column 2 of what?
A simplification of your code:
imgFiles = dir('*.jpg');
numFiles = numel(imgFiles); % NUMEL is safer than LENGTH
valueOfA = zeros(1, numFiles);
for k = 1:numFiles - 1
img = imread(imgFiles(k).name);
compareimg = imread(imgFiles(k + 1).name);
P = imabsdiff(img,compareimg);
figure;
imshow(P,[]);
valueOfA(k) = nnz(P);
end
swati mane
2019 年 7 月 19 日
編集済み: swati mane
2019 年 7 月 19 日
Thank you for your help. But not getting the result.
my question is when i take 8 frames using this code i got correct answer. But when i include the frames from 9 to 12. When i observe column 1...2..3..The values in the columns of array A are changed.
As i am taking the difference of two frames.The difference should be constant although i am adding more and more frames in the folder.
Joel Handy
2019 年 7 月 19 日
編集済み: Joel Handy
2019 年 7 月 19 日
It sounds like maybe you are getting the same or simiiar output, just the order of the values is changing.
Look at the order of imgFiles when you run with 8 images and when you run with 9 images. My guess is that the order of the file list that the call to dir gives you changes. What are the names of these image files?
swati mane
2019 年 7 月 19 日
Sir ..thanks for your reply. I understood that it is a vector.
When i take difference of pixels of frame 1 and frame 2 ..i got some value. And i think that value should be constant. When i have 9 frames in my folder i have constant value of difference. But when i add frame 10 in my folder then the difference of frame 1 and frame 2 changed. Why it is so?
Joel Handy
2019 年 7 月 19 日
Because the file list returned by "dir" is sorted more or less alphabetically (it does capital letters before lower case ones). If I have 9 images:Img1.jpg, Img2.jpg, Img3.jpg, etc, dir will return:
Img1.jpg, Img2.jpg, Img3.jpg, Img4.jpg, Img5.jpg, Img6.jpg, Img7.jpg, Img8.jpg, Img9.jpg
If I add an new file with name Img10.jpg, it will return:
Img1.jpg, Img10.jpg, Img2.jpg, Img3.jpg, Img4.jpg, Img5.jpg, Img6.jpg, Img7.jpg, Img8.jpg, Img9.jpg
swati mane
2019 年 7 月 19 日
編集済み: swati mane
2019 年 7 月 19 日
@joel handy ...now I got what happend with my code.
Thank you so much.
swati mane
2019 年 7 月 19 日
If i want to take files 1 to 500 ..in the sequence ..instead of dir ..what should be used?
Joel Handy
2019 年 7 月 19 日
編集済み: Joel Handy
2019 年 7 月 19 日
One option is to rename your files. If you named them something like Img001, img002, .., Img498, Img499, Img500, dir would return your file names in the right order.
You might be able to sort your images by date. The file info that dir gives you has the files' datestamp. That can be unreliable though if people are copying images around or opening and closing them. Anything that would update the files metadata.
The most reliable but most complicated option would be to parse you file names to extract the file number and sort that way.
imgFiles=dir('*.jpg');
fileNames = {imgFiles.name}'
fileNumStr = regexp(fileNames, '[0-9]*', 'match');
fileNum = str2double([fileNumStr{:}]);
[~,sortIdx] = sort(fileNum);
imgFiles = imgFiles(sortIdx);
swati mane
2019 年 7 月 24 日
Hello sir..
Now i have frames named as 000 to 505 .
I want to take these frames as 000 to 025 then 026 to 050 then 051 to 075 and so on.
Per second i should have 25 frames. Total time is 20 seconds. Thanks in advance.
採用された回答
Jan
2019 年 7 月 19 日
Prefer file names including leading zeros: image0001.jpg, image 0002.jpg etc. Then the numeical order equals the alphabetical order.
If you have the less useful naming scheme already, use https://www.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort to get it in order.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)