any one help me to correct this code or help me my I have a graduation project coming soon
이전 댓글 표시
clear; close all; clc;
warning('off', 'all');
[contentFile, contentPath] = uigetfile({'*.bmp;*.jpg;*.png;*.tif', 'ملفات الصور'}, 'اختر صورة المحتوى');
if isequal(contentFile, 0)
error('لم يتم اختيار صورة محتوى');
end
contentImage = imread(fullfile(contentPath, contentFile));
[styleFile, stylePath] = uigetfile({'*.bmp;*.jpg;*.png;*.tif', 'ملفات الصور'}, 'اختر صورة النمط');
if isequal(styleFile, 0)
error('لم يتم اختيار صورة نمط');
end
styleImage = imread(fullfile(stylePath, styleFile));
contentImage = im2double(contentImage);
styleImage = im2double(styleImage);
if size(contentImage,3) == 1
contentImage = repmat(contentImage, [1 1 3]);
end
if size(styleImage,3) == 1
styleImage = repmat(styleImage, [1 1 3]);
end
styleImage = imadjust(styleImage, [0.1 0.9], []);
styleImage = imgaussfilt(styleImage, 1);
net = vgg19();
inputSize = net.Layers(1).InputSize(1:2);
% تغيير حجم الصور مع الحفاظ على التناسب
contentImage = imresize(contentImage, inputSize);
styleImage = imresize(styleImage, inputSize);
%% 4. تحديد الطبقات الصحيحة
contentLayer = 'conv4_2';
styleLayers = {'conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1'};
styleFeatures = getStyleFeatures(styleImage, net, styleLayers);
numIterations = 500;
learningRate = 0.01;
styleWeight = 1e6;
dlContent = dlarray(contentImage, 'SSC');
dlTransform = dlContent;
figure;
for iter = 1:numIterations
[grad, loss] = dlfeval(@computeGradients, dlTransform, dlContent, net, ...
contentLayer, styleLayers, styleFeatures, styleWeight);
dlTransform = dlTransform - learningRate * grad;
if mod(iter,50) == 0 || iter == 1
fprintf('التكرار %d: الخسارة الكلية=%.2f, المحتوى=%.2f, النمط=%.2f\n', ...
iter, loss.total, loss.content, loss.style);
% عرض التقدم
currentImg = uint8(extractdata(dlTransform)*255);
imshow(currentImg);
title(sprintf('التكرار %d/%d', iter, numIterations));
drawnow;
end
end
outputImage = uint8(extractdata(dlTransform)*255);
[~,name,ext] = fileparts(contentFile);
outputFile = fullfile(pwd, [name '_styled' ext]);
imwrite(outputImage, outputFile);
fprintf('تم حفظ الصورة الناتجة بنجاح في: %s\n', outputFile);
function features = getStyleFeatures(styleImg, net, styleLayers)
if ~ismatrix(styleImg) && ~(ndims(styleImg)==3)
error('يجب أن تكون صورة النمط مصفوفة 2D أو 3D');
end
if size(styleImg,3) ~= 3
error('يجب أن تحتوي صورة النمط على 3 قنوات لونية (RGB)');
end
try
dlStyle = dlarray(styleImg, 'SSC');
catch
error('فشل تحويل صورة النمط إلى dlarray');
end
features = struct();
for i = 1:length(styleLayers)
layer = styleLayers{i};
try
dlFeatures = activations(net, dlStyle, layer);
features.(layer) = computeGramMatrix(dlFeatures);
catch ME
error('فشل في استخراج خصائص الطبقة %s: %s', layer, ME.message);
end
end
end
function gramMatrix = computeGramMatrix(features)
[H,W,C] = size(features);
reshaped = reshape(features, H*W, C);
gramMatrix = reshaped' * reshaped / (H*W*C);
end
function [gradients, loss] = computeGradients(dlTransform, dlContent, net, ...
contentLayer, styleLayers, styleFeatures, styleWeight)
contentFeatures = activations(net, dlContent, contentLayer);
transformContentFeatures = activations(net, dlTransform, contentLayer);
contentLoss = mean((transformContentFeatures - contentFeatures).^2);
styleLoss = 0;
for i = 1:length(styleLayers)
layer = styleLayers{i};
transformFeatures = activations(net, dlTransform, layer);
gramTransform = computeGramMatrix(transformFeatures);
gramStyle = styleFeatures.(layer);
styleLoss = styleLoss + mean((gramTransform - gramStyle).^2);
end
styleLoss = styleLoss / length(styleLayers);
totalLoss = contentLoss + styleWeight * styleLoss;
gradients = dlgradient(totalLoss, dlTransform);
loss.total = double(totalLoss);
loss.content = double(contentLoss);
loss.style = double(styleLoss);
end
%%%%
erorr
rror using styletransfer>getStyleFeatures (line 111)
فشل في استخراج خصائص الطبقة conv1_1: Invalid 2-D image data. Specify image data as a 3-D numeric array containing a single image, a
4-D numeric array containing multiple images, a datastore, or a table containing image file paths or images in the first column.
Error in styletransfer (line 48)
styleFeatures = getStyleFeatures(styleImage, net, styleLayers);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
댓글 수: 24
Image Analyst
2025년 4월 27일
Looks like one of the arguments is not what it expects. What does this show in the command window?
whos styleImage
whos net
whos styleLayers
osama
2025년 4월 28일
Cris LaPierre
2025년 4월 28일
Please attach your 2 images to your post so we can test your code. You can attach them using the paperclip icon.
Walter Roberson
2025년 4월 28일
styleImage 384x512x3 2359296 single
I wonder if the problem is that styleImage is datatype single instead of uint8 or double ?
Image Analyst
2025년 4월 29일
@osama unless you know for a fact that your network requires floating point images, comment out these two lines
contentImage = im2double(contentImage);
styleImage = im2double(styleImage);
and see how it goes.
osama
2025년 4월 30일
Walter Roberson
2025년 5월 1일
Your code is for bmp files, but you have posted two jpg images and no bmp images.
It is not clear which image is intended to be the "Content" image.
osama
2025년 5월 1일
Walter Roberson
2025년 5월 1일
It is not clear which image is intended to be the "Content" image. It is not clear which images are intended to be within the style folder.
osama
2025년 5월 1일
Walter Roberson
2025년 5월 1일
I am running the code now. It is taking a fair while.
Walter Roberson
2025년 5월 2일
편집: Walter Roberson
2025년 5월 2일
I made the qlf file into a bmp file and put it inside a directory named "styles".
I made the non-qlf file into a bmp file, and gave the name of that file in response to the uigetfile()
The code ran without producing any error messages. It tooks more than an hour to run on my machine.
At this point, I do not know what I should be looking for?
Note: I ran on R2025a Pre-release on an intel iMac.
osama
2025년 5월 2일
osama
2025년 5월 2일
Walter Roberson
2025년 5월 3일
I do not know what to look for to determine whether there is transfer of style, but the final image did not have noticeable mixing of the other image.
I prefer to handle discussions in public.
osama
2025년 5월 3일
Walter Roberson
2025년 5월 3일
If I imresize() the original image to be the same size as the output image, then I am able to compare the output image to the original image. The output image has slightly different lighting compared to the input image -- a little pinker, less stark white.
Walter Roberson
2025년 5월 3일
I do not understand what it means for the "style" to be "transferred".
Is the idea that you want to end up with the teeth isolated and the gums dimmed out? I don't think your current code attempts that; your current code does not attempt to isolate the teeth, and so could easily find "features" in the gums area.
osama
2025년 5월 3일
Walter Roberson
2025년 5월 3일
That makes it sounds as if you just want to copy the style image to the regular image.
What is it about the style image that you want merged with the regular image? Saying "the style properties" is too general.
osama
2025년 5월 4일
Walter Roberson
2025년 5월 5일
No-one is going to be able to assist you unless you explain what it means to you to "transfer style properties" .
Are you trying to end up with the teeth unchanged but the gums a monochrome red ?
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Built-In Training에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!